20分求大佬调整,谢谢

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

zhang33 @ 2024-11-14 16:13:52

#include <bits/stdc++.h>
using namespace std;
int n;
struct student{
    string name;
    int chineses,math,english;
}now,top;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>now.name>>now.chineses>>now.math>>now.english;
        if(now.chineses>>now.english>>now.math>top.chineses+top.english+top.math||i==1){
            top=now;
        }
    }
    cout<<top.name<<" "<<top.chineses<<" "<<top.math<<" "<<top.english;
    return 0;
}

by Pure_Echo @ 2024-11-14 16:36:43

@zhang33你这个判断有问题


by Pure_Echo @ 2024-11-14 16:38:07

你要考虑到判断的符号优先级问题

#include <bits/stdc++.h>
using namespace std;
int n;
struct student{
    string name;
    int chineses,math,english;
}now,top;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>now.name>>now.chineses>>now.math>>now.english;
        if((now.chineses+now.english+now.math>top.chineses+top.english+top.math)||i==1){
            top=now;
        }
    }
    cout<<top.name<<" "<<top.chineses<<" "<<top.math<<" "<<top.english;
    return 0;
}

|