描述: 给定一组记录n(n<100)小明各个时期的考试成绩 ,格式为日期+成绩,中间以空格隔开,记录之间
分行输入 ,例如
2008/6/3 80
2009/1/1 56
….
其中日期输入要求年份1996-2100 月份1-12 日期1-31
现要求以分数为关键字从大到1小对其进行排序,若分数相同则按日期从小到大排序
输入样例
4
2017/1/1 95
2017/6/10 85
2017/3/2 95
2017/1/1 65
输出样例
2017/1/1 95
2017/3/2 95
2017/6/10 85
2017/1/1 65
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct record{
string date;
int grade;
};
bool check_valid(int year, int month, int day) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days[2] = 29;
} else {
days[2] = 28;
}
if (year < 1996 || year > 2100 || month < 1 || month > 12 || day < 1 || day > days[month]) {
return false;
}
return true;
}
bool cmp(record x, record y) {
if (x.grade != y.grade) {
return x.grade > y.grade;
}
return x.date < y.date;
}
int main() {
int n;
cin >> n;
record records[n]; // 定义结构体数组来存储记录
for (int i = 0; i < n; i++) {
cin >> records[i].date >> records[i].grade;
}
sort(records, records + n, cmp);
for (int i = 0; i < n; i++) {
int year, month, day;
sscanf(records[i].date.c_str(), "%d/%d/%d", &year, &month, &day);
if (check_valid(year, month, day)) {
cout << records[i].date << " " << records[i].grade << endl;
}
}
return 0;
}