90分谢了!

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

s________b @ 2024-08-15 15:45:52

为什么错了?```cpp

include <iostream>

include <string>

using namespace std;

struct student{

string name;
int chinese,math,english;
int z=chinese+math+english;

}a,ans;

int main(){

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;

}


by Ian_NIE @ 2024-08-15 15:54:45

@s____b 结构体里不能直接赋值,没有这种写法,有你的Chinese math和English也都是0。你得在程序里单独计算


by qiaochu @ 2024-08-15 16:01:48

要不试试我的

#include<bits/stdc++.h>
using namespace std;
struct st{
  string name;
  int ch;
  int maths;
  int eng;
  int score;
};
int main()
{
    st s[1001];
    int n,max=0,maxi=1;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i].name>>s[i].ch>>s[i].maths>>s[i].eng;
        s[i].score=s[i].ch+s[i].maths+s[i].eng;
    }
    max=s[1].score;
    for(int i=2;i<=n;i++)
    {
        if(s[i].score>max)max=s[i].score,maxi=i;
    }
    cout<<s[maxi].name<<" "<<s[maxi].ch<<" "<<s[maxi].maths<<" "<<s[maxi].eng;
    return 0;[AC记录](https://www.luogu.com.cn/record/161663906)    
}

by zhizhenhuyuzhe @ 2024-08-15 16:13:58

#include<cstdio>
#include<iostream>
using namespace std;
string name[1005];
int mann[1005],man[1005],mannn[1005];
int main(){
    int n,max=-999999,t;
    scanf("%d",&n);
    for(int a=0;a<n;a++){
        cin>>name[a]>>mann[a]>>man[a]>>mannn[a];
    }
    for(int b=0;b<n;b++){
        if(mann[b]+man[b]+mannn[b]>max){
            max=mann[b]+man[b]+mannn[b];
            t=b;
        }
    }
    cout<<name[t]<<" "<<mann[t]<<" "<<man[t]<<" "<<mannn[t];
    return 0;
}

by s________b @ 2024-08-16 09:43:51

#include <iostream>
#include <string>
using namespace std;
struct student{
    string name;
    int chinese,math,english;
}a,ans;

int main(){
    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;
}

这样呢?


by s________b @ 2024-08-16 12:09:49

@qiaochu thank you very much!!


by qiaochu @ 2024-08-16 13:20:28

@s____b You are welcome.


|