Written for: "Informatics Olympiad a pass" on Chapter 8 exercise machine.
Title Description
Doctor patient level, a program written in the registration order doctor patients discharged on the following principles:
- The elderly (aged \ (\ GE \) 60 years of age) than non-elderly priority doctor.
- Elderly doctor descending order according to age, the age of the same sort by the order of registration.
- Non-elderly sorted in chronological order of registration.
Input Format
The first \ (1 \) line, an input is not greater than \ (1000 \) positive integer representing the number of patients;
behind the patient in accordance with the order of registration, each line of input information of a patient, comprising: a length of not greater than \ (10 \) string representation patient ID (patient ID for each different and each contains only numbers and letters), age of the patient represents an integer, separated by a single space.
Output Format
Good sequentially outputted row by doctor patient ID, one per line.
Sample input
5
020010 40
004312 15
010133 67
023815 75
102031 23
Sample Output
023815
010133
020010
004312
102031
Topic analysis
This question is ordered structure.
First question we need to think about how the definition of the structure: Because the patient's ID contains numbers and characters, so I use an integer number
to indicate the order of registration of patients, with an array of characters card
to represent the patient's ID, use an integer age
to represent patients age. "Patient" in English is "patient", so I called open Patient
structure and definition of the structure while initializing array of structures (I use an array a
representation):
struct Patient {
int number; // 登记的顺序
char id[11]; // 病人ID
int age; // 年龄
} a[1010];
Comparison function accordance with the subject description:
bool cmp(Patient a, Patient b) {
if (a.age >= 60 && b.age >= 60) { //a,b都是老年人
if (a.age != b.age) // 如果年龄不一样
return a.age > b.age; // 按年龄从大到小排
else // 否则,年龄一样
return a.number < b.number; // 按登记顺序排
}
else if (a.age >= 60) // a是老人,b不是老人
return true; // a排前面
else if (b.age >= 60) // b是老人,a不是老人
return false; // b排前面
else // 两个都是年轻人
return a.number < b.number; // 按登记顺序排
}
Note: Because the input format only tells us the patient's ID and age, does not tell us the order of registration, so we have to take the initiative to order registration of marks for each patient, the easiest way is to first \ (I \) registration sequence patients is \ (I \) .
Codes are as follows:
#include <bits/stdc++.h>
using namespace std;
struct Patient {
int number; // 登记的顺序
char id[11]; // 病人ID
int age; // 年龄
} a[1010];
int n;
bool cmp(Patient a, Patient b) {
if (a.age >= 60 && b.age >= 60) { //a,b都是老年人
if (a.age != b.age) // 如果年龄不一样
return a.age > b.age; // 按年龄从大到小排
else // 否则,年龄一样
return a.number < b.number; // 按登记顺序排
}
else if (a.age >= 60) // a是老人,b不是老人
return true; // a排前面
else if (b.age >= 60) // b是老人,a不是老人
return false; // b排前面
else // 两个都是年轻人
return a.number < b.number; // 按登记顺序排
}
int main() {
cin >> n;
for (int i = 0; i < n; i ++) {
cin >> a[i].id >> a[i].age;
a[i].number = i; // 用 i 来表示 第 i 个病人的登记顺序
}
sort(a, a+n, cmp);
for (int i = 0; i < n; i ++)
cout << a[i].id << endl;
return 0;
}