Drin_love2003 @ 2019-11-01 10:45:04
我开单步调试确定可能是sort()的传参问题,哪位大佬能给我解释一下?
#include<iostream>
#include<algorithm>
#include <string>
using namespace std;
struct stu
{
int num, chn, mah, eng;
};
bool c(stu a, stu b)
{
if (a.chn + a.mah + a.eng > b.chn + b.mah + b.eng)
return true;
else if (a.chn > b.chn)
return true;
else if (a.num < b.num)
return true;
else return false;
}
int main()
{
int n;
stu s[300];
cin >> n;
for (int i = 0; i < n; i++)
{
s[i].num = i + 1;
cin >> s[i].chn >> s[i].mah >> s[i].eng;
}
sort(s, s + n, c);
for (int i = 0; i < 5; i++)
cout << s[i].num << " " << s[i].chn + s[i].mah + s[i].eng << endl;
return 0;
}
by big_news @ 2019-11-01 11:03:06
为什么我找不到您的评测记录?
by 绿萧 @ 2019-11-01 11:12:24
@Drin_love2003
你的 函数c 写错了,而且数组要再开大一点,不然 sort 中 s+n 可能会越界
by Drin_love2003 @ 2019-11-01 12:13:16
@绿萧 那应该咋写
by Drin_love2003 @ 2019-11-01 12:13:52
@big_news 我用的Visual Studio,这么些总是异常
by 绿萧 @ 2019-11-01 13:35:34
@Drin_love2003
bool c(stu a, stu b)
{
if (a.chn + a.mah + a.eng != b.chn + b.mah + b.eng)
return a.chn + a.mah + a.eng > b.chn + b.mah + b.eng;
else if (a.chn != b.chn)
return a.chn > b.chn;
else if (a.num != b.num)
return a.num < b.num;
else return false;
}
一定要等于才else,不然如果总分a比b小,但语文a比b大,你就返回true了
by Drin_love2003 @ 2019-11-02 22:12:53
@绿萧 THANK YOU SIR