Cryflmind @ 2023-09-24 21:14:01
写程序的时候发现一个有意思的地方:
如果你在struct中使用了string,这个时候使用sort就会被爆RE,但是当你把string换成了char[],这个题他就过了。
问题是,哪位大佬能给我解释下这个原因,是因为string长度是可变的吗?
by Jim_Franklin @ 2023-09-24 21:17:14
为什么要用sort啊,不是大才小用吗?
by OIerWu_829 @ 2023-09-24 21:19:32
结构体排序 AC Code
#include <bits/stdc++.h>
using namespace std;
struct node
{
string name;
int s, ch, ma, en, id;
}a[1010];
bool cmp(node x, node y)
{
if (x.s != y.s) return x.s > y.s;
return x.id < y.id;
}
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> a[i].name >> a[i].ch >> a[i].ma >> a[i].en;
a[i].s = a[i].ch + a[i].ma + a[i].en;
a[i].id = i;
}
sort(a + 1, a + n + 1, cmp);
cout << a[1].name << " " << a[1].ch << " " << a[1].ma << " " << a[1].en;
return 0;
}
by xiaofeng_and_xiaoyu @ 2023-09-24 21:21:55
这题不需要用排序吧。就算需要,也用不着排字符串吧。
by Cryflmind @ 2023-09-24 21:22:44
???那我这是怎么回事
#include<bits/stdc++.h>
using namespace std;
struct stu{
string name;
int c,m,e,idx,sums;
}s[1050];
long long n;
bool cmp(stu a,stu b)
{
if(a.sums!=b.sums) return a.sums>b.sums;
else return a.idx<b.idx;
}
int main()
{
cin>>n;
for(long long i=1;i<=n;i++)
{
scanf("%s %d %d %d",&s[i].name,&s[i].c,&s[i].m,&s[i].e);
s[i].idx=i;
s[i].sums=s[i].c+s[i].m+s[i].e;
}
sort(s+1,s+n+1,cmp);
cout<<s[1].name<<" "<<s[1].c<<" "<<s[1].m<<" "<<s[1].e;
return 0;
}
所有测试点全部RE@wzj0829
by Cryflmind @ 2023-09-24 21:23:46
@Jim_Franklin @xiaofeng_and_xiaoyu 我也没排字符串啊xwx
评测记录
by Cryflmind @ 2023-09-24 21:27:08
离谱的是,我后面改了个string name
把它改成char name[10],然后就AC了
by xiaofeng_and_xiaoyu @ 2023-09-24 21:30:36
@Cryflmind 把scanf换成cin就能A
by xiaofeng_and_xiaoyu @ 2023-09-24 21:32:03
建议直接迭代
by Cryflmind @ 2023-09-24 21:33:34
@xiaofeng_and_xiaoyu ???合着是scanf导致的???
但是为啥owo
by 1073741824aa @ 2023-10-13 19:50:22
sort不稳定