病人登记看病,编写一个程序,将登记的病人按照以下原则排出看病的先后顺序:
1. 老年人(年龄 >= 60岁)比非老年人优先看病。
2. 老年人按年龄从大到小的顺序看病,年龄相同的按登记的先后顺序排序。
3. 非老年人按登记的先后顺序看病。
后面按照病人登记的先后顺序,每行输入一个病人的信息,包括:一个长度小于10的字符串表示病人的ID(每个病人的ID各不相同且只含数字和字母),一个整数表示病人的年龄,中间用单个空格隔开。
5 021075 40 004003 15 010158 67 021033 75 102012 30
参考代码
#include <string.h> #include <string> using std::string; #include <iostream> using namespace std; struct paitent{ int age; char id[10]; int sequence; }; int Compare(paitent a, paitent b){ if (a.age >= 60 && b.age >= 60){ if (a.age < b.age){ return true; } else if (a.age == b.age){ if (a.sequence > b.sequence ){ return true; } } } if (a.age < 60 && b.age < 60) { if (a.sequence > b.sequence){ return true; } } if (a.age < 60 && b.age >= 60) { return true; } return false; } void PrintResult(paitent *s,int n){ for (int i =0;i<n;i++){ cout << s[i].id << endl; } } void PaitentSort (paitent *s,int num){ paitent tmp; for(int i=0 ; i < num-1;i++){ for (int j=0;j<num-1-i;j++){ if (Compare(s[j],s[j+1])){ tmp = s[j]; s[j] = s[j+1]; s[j+1] = tmp ; } } } } int main() { //freopen("C:\\Users\\Administrator\\Desktop\\mooc.txt","r",stdin); int n; paitent s[100]; cin >> n ; for (int i =0;i<n;i++){ s[i].sequence = i; cin >> s[i].id >>s[i].age; } PaitentSort(s,n); PrintResult(s,n); return 0; }
021033 010158 021075 004003 102012