2 solutions

  • 1
    @ 2025-1-13 10:41:39
    
    #include<bits/stdc++.h>
    using namespace std;
    struct Stu
    {
        string x;
        int id;
        int age;    
    }; 
    Stu s[110];
    bool cmp(Stu a,Stu b)
    {
        if(a.age>=60&&b.age<60) return 1; //老年人在前 
        if(a.age>=60&&b.age>=60&&a.age>b.age) return 1; //都是老年人,按照年龄 
        if(a.age>=60&&b.age>=60&&a.age==b.age&&a.id<b.id) return 1; //都是老年人,年龄也相同,按照编号 
        if(a.age<60&&b.age<60&&a.id<b.id) return 1; //非老年人,按照编号 
        return 0;
    }
    int main()
    {
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            s[i].id=i;
            cin>>s[i].x>>s[i].age;
        }
        sort(s,s+n,cmp);
        for(int i=0;i<n;i++)
            cout<<s[i].x<<endl;
        return 0;
    }
    • 0
      @ 2024-7-5 11:37:21
      #include<bits/stdc++.h>
      using namespace std;
      struct Stu
      {
          string x;
          int id;
          int age;    
      }; 
      Stu s[110];
      bool cmp(Stu a,Stu b)
      {
          if(a.age>=60&&b.age<60) return 1; //老年人在前 
          if(a.age>=60&&b.age>=60&&a.age>b.age) return 1; //都是老年人,按照年龄 
          if(a.age>=60&&b.age>=60&&a.age==b.age&&a.id<b.id) return 1; //都是老年人,年龄也相同,按照编号 
          if(a.age<60&&b.age<60&&a.id<b.id) return 1; //非老年人,按照编号 
          return 0;
      }
      int main()
      {
          int n;
          cin>>n;
          for(int i=0;i<n;i++)
          {
              s[i].id=i;
              cin>>s[i].x>>s[i].age;
          }
          sort(s,s+n,cmp);
          for(int i=0;i<n;i++)
              cout<<s[i].x<<endl;
          return 0;
      }
      
      • 1

      Information

      ID
      948
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      3
      Tags
      (None)
      # Submissions
      49
      Accepted
      17
      Uploaded By