iqiqiqiqiqiqiqiq @ 2024-06-21 21:54:31
好多次了,一直RE,好烦
#include<bits/stdc++.h>
using namespace std;
struct student {
string name;
int y, m, d;
}c[105];
bool cmp(student a, student b) {
if (a.y != b.y)
return a.y < b.y;
if (a.m != b.m)
return a.m < b.m;
return a.d <= b.d;
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i].name >> c[i].y >> c[i].m >> c[i].d;
}sort(c + 1, c + n + 1, cmp);
for (int i = n; i > 0; i++) {
cout << c[i].name << endl;
}return 0;
}
有问题吗...
by Special_Tony @ 2024-06-21 21:56:37
@iqiqiqiqiqiqiqiq 温馨提示:sort要求cmp函数必须是严格小于(或大于),不能有<=和>=出现哦!然后原理自己搜
by CEFqwq @ 2024-06-21 21:57:04
@Special_Tony 是因为不稳定吗
by CEFqwq @ 2024-06-21 21:57:23
这个我还真不知道
by Fish_Love_Water @ 2024-06-21 21:58:58
@iqiqiqiqiqiqiqiq 我觉得是很有问题的
for (int i = n; i > 0; i++) {
cout << c[i].name << endl;
}
这里应该是
for(int i=1;i<=n;i++) cout << c[i].name << endl;
by _l_l_ @ 2024-06-21 21:58:58
@CEFqwq 因为就是这样,否则是未定义行为
by Special_Tony @ 2024-06-21 21:59:01
@CEFqwq 不知道,反正之前看到过有人也掉坑了,然后有位dalao解释了
by Steve_xh @ 2024-06-21 21:59:46
@iqiqiqiqiqiqiqiq
for (int i = n; i > 0; i++)
i++
by Special_Tony @ 2024-06-21 22:00:21
@Fish_Love_Water cwd,不是这里,lz是从小到大排序然后倒序输出。
by Special_Tony @ 2024-06-21 22:00:58
@iqiqiqiqiqiqiqiq 当然楼上的i++也是个问题
by Steve_xh @ 2024-06-21 22:01:01
@Special_Tony 用 stable_sort
就行吧