病人排队

【问题描述】

病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序:

  1.老年人(年龄 >= 60岁)比非老年人优先看病。

  2.老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。

  3.非老年人按登记的先后顺序看病。

【输入格式】

   第1行,输入一个小于100的正整数,表示病人的个数;后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID(每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,中间用单个空格隔开。

【输出格式】

   按排好的看病顺序输出病人的ID,每行一个。

【输入样例】

  5
  021075 40
  004003 15
  010158 67
  021033 75
  102012 30

【输出样例】

  021033
  010158
  021075
  004003
  102012

#include<iostream>
#include<algorithm>
using namespace std;

struct people{
    string id;
    int age, num; 
};
bool cmp(people x, people y){
    if(x.age>=60||y.age>=60) {
        if(x.age!=y.age) return x.age > y.age;
        else return x.num < y.num;
    }else{
        return x.num < y.num;
    }
}
int main(){
    int n;
    cin>>n;
    people a[100];
    for(int i=0; i<n; i++){
        cin>>a[i].id>>a[i].age;
        a[i].num=i+1;
    }
    sort(a, a+n, cmp);
    for(int i=0; i<n; i++){
        cout<<a[i].id<<endl;
    }
    return 0;
} 

 

posted @ 2022-07-18 13:15  Hi,小董先生  阅读(427)  评论(0)    收藏  举报