题目
思路
思路就是利用结构体来存储对应学生的信息,然后利用一个cmp函数对结构体进行排序,注意我的结构体遍历时从1开始的,所以应该是sort(S+1,S+1+n),因为比较习惯从1开始hhhh 。后面利用双层循环遍历输出即可。
AC代码
#include <bits/stdc++.h>
using namespace std;
struct student {
char number[11];
int month;
int day;
} S[210];
bool cmp(student s1, student s2) {
if (s1.month != s2.month) {
return s1.month < s2.month;
} else {
return s1.day < s2.day;
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%s %d %d", S[i].number, &S[i].month, &S[i].day);
}
sort(S + 1, S + n + 1, cmp);
int i, j;
for (int i = 1; i <= n; ++i) {
if (j == n + 1)
return 0;
cout << S[i].month << " " << S[i].day << " "; //先输出月份与日期
for (j = i; j <= n; ++j) {
if (S[i].month == S[j].month && S[i].day == S[j].day) {
cout << S[j].number << " ";
} else {
cout << endl;
i = j - 1;
break;
}
}
}
}