大佬们90分第一个测试点过不去求助

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

liuyuxi2011 @ 2023-04-17 23:02:03

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    string s,ss;
    int a,b,c,l=0,m=0,ad=0,bd=0,cd=0;
    for(int i=1;i<=n;i++){
        m=0;
        cin>>s>>a>>b>>c;
        m=a+b+c;
        if(m>l){
            l=m;
            ss=s;
            ad=a;
            bd=b;
            cd=c;
        }
    }
    cout<<ss<<" "<<ad<<" "<<bd<<" "<<cd;
    return 0;
}

by Wangzj512 @ 2023-04-17 23:38:43

@xiaoliuyimei l 的初始值应为-1,否则如果全是0分,程序输出绝妙的答案就会WA


by 1ooper @ 2023-05-02 16:50:48

还是得参考我改的

#include<bits/stdc++.h>
using namespace std;
int main(){

    int n;
    cin>>n;
    string s,ss;
    int a,b,c,l=-1000,m=0,ad=0,bd=0,cd=0;
    for(int i=1;i<=n;i++){
        m=0;
        cin>>s>>a>>b>>c;
        m=a+b+c;
        if(m>l){
            l=m;
            ss=s;
            ad=a;
            bd=b;
            cd=c;
        }
    }
    cout<<ss<<" "<<ad<<" "<<bd<<" "<<cd;
    return 0;
}

by 1ooper @ 2023-05-03 23:03:15

其实不建议使用这种方法,体现不出你的编程能力,尽量使用结构体与自定义函数可能会好一点


by Tim_the_best @ 2023-06-11 14:14:03

#include<bits/stdc++.h>

using namespace std;
struct student{
    string name;
    int chinese,math,english;
}a,ans;
int main() {
    ans.chinese=-100;
    ans.math=-100;
    ans.english=-100;
int n;
cin>>n;
    for (int i = 1; i <= n; ++i) {
        cin>>a.name>>a.chinese>>a.math>>a.english;
        if (a.chinese+a.math+a.english>ans.chinese+ans.math+ans.english){
            ans=a;
        }
    }
    cout<<ans.name<<" "<<ans.chinese<<" "<<ans.math<<" "<<ans.english;
    return 0;
    }

好一点的代码


|