我连样例都过不了,哪位闭月羞花或英俊帅气的大佬愿意帮帮我吗?

P5740 【深基7.例9】最厉害的学生

Yunjin @ 2021-03-28 20:46:59

#include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<cmath>
using namespace std;
struct stu{
    char name[8];
    int ch,ma,en;
    int all;
};
bool cmp(stu t1,stu t2){
    return t1.all>t2.all;
}
int main(){
    int shuliang = 0;
    stu a[1005];
    cin>>shuliang;
    for(int i = 0;i < shuliang;i++){
        cin>>a[i].name>>a[i].ch>>a[i].ma>>a[i].en;
        a[i].all = a[i].ch+a[i].ma+a[i].en;
    }
    sort(a,a+shuliang,cmp);
    cout<<a[1].name<<" "<<a[1].ch<<" "<<a[1].ma<<" "<<a[1].en; 

    return 0;
}//谁可以帮我看看错在哪了/(T-T)\

by _短笛无腔_ @ 2021-03-28 20:49:20

闭月羞花指的是女孩子啊


by _短笛无腔_ @ 2021-03-28 20:50:52

@wx310756 输出写错了

改成这样

cout<<a[0].name<<" "<<a[0].ch<<" "<<a[0].ma<<" "<<a[0].en; 

by Yunjin @ 2021-03-28 20:54:09

谢谢各位大佬!


by Maxmilite @ 2021-03-28 20:56:07

简单改了四五个细节

代码先贴在这 不懂的可以at我

#include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<cmath>
using namespace std;
struct stu{
    // changed a line here
    char name[9];
    int ch,ma,en;
    // changed a line here
    int all, num;
};
bool cmp(stu t1,stu t2){
    // added two lines here
    if (t1.all == t2.all)
        return t1.num < t2.num;
    return t1.all>t2.all;
}
int main(){
    int shuliang = 0;
    stu a[1005];
    cin>>shuliang;
    for(int i = 0;i < shuliang;i++){
        cin>>a[i].name>>a[i].ch>>a[i].ma>>a[i].en;
        // added a line here
        a[i].num = i;
        a[i].all = a[i].ch+a[i].ma+a[i].en;
    }
    sort(a,a+shuliang,cmp);
    // changed a line here
    cout<<a[0].name<<" "<<a[0].ch<<" "<<a[0].ma<<" "<<a[0].en << endl; 

    return 0;
}//谁可以帮我看看错在哪了/(T-T)\

by Maxmilite @ 2021-03-28 20:56:15

@wx310756


by Yunjin @ 2021-03-28 20:59:54

谢谢大家!我过啦! @(^-^)@


by ZBAA_MKC @ 2021-03-28 21:18:25

@wx310756 stable_sort()会更好一些


|