第一个点WA,TAT

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

OIer_Kevin @ 2023-02-26 09:57:25

第一个点WA,QAQ

#include<iostream>
using namespace std;
struct classa{
   string name;
   int ch,ma,en; 
}a,ans;
int main(){
   int n;
   cin>>n;
   for(int i=1;i<=n;i++){
       cin>>a.name>>a.ch>>a.ma>>a.en;
       if(a.ch+a.ma+a.en>ans.ch+ans.ma+ans.en){
           ans=a;
       }
   }
   cout<<ans.name<<" "<<ans.ch<<" "<<ans.ma<<" "<<ans.en<<endl;
   return 0;
}

by LEle0309 @ 2023-02-26 09:57:51

sofa


by LEle0309 @ 2023-02-26 09:58:40

参考一下


#include<iostream>
using namespace std;
int n,maxx=-1,idx;
struct stu
{
    char n[10];
    int a,b,c,s;
}a[1005]; 
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].n>>a[i].a>>a[i].b>>a[i].c;
        a[i].s=a[i].a+a[i].b+a[i].c;
        if(a[i].s>maxx)
        {
            maxx=a[i].s;
            idx=i;
        }
    }
    cout<<a[idx].n<<" "<<a[idx].a<<" "<<a[idx].b<<" "<<a[idx].c;
    return 0;
}

by LEle0309 @ 2023-02-26 10:00:34

ans结构体没初始化 @ganyudetinali


by OIer_Kevin @ 2023-02-26 10:05:38

?????????????


by LEle0309 @ 2023-02-26 10:06:58

@ganyudetinali

ans={abc,-1,-1,-1}//结构体初始化,类似数组

或者

int maxx=-1;//结构体里加一个sum,表示总和,然后打擂台

求关注


by 1ooper @ 2023-05-03 23:07:38

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
struct node{
    long long x,w,s,y,ans;
    string na;
}k[N];
bool cmp(node a,node z){
    if(a.ans==z.ans)return a.x<z.x;
    return a.ans>z.ans;
}
int main(){
    long long n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>k[i].na>>k[i].w>>k[i].s>>k[i].y;
        k[i].ans=k[i].w+k[i].s+k[i].y;
        k[i].x=i;
    }
    sort(k+1,k+1+n,cmp);
    cout<<k[1].na<<" "<<k[1].w<<" "<<k[1].s<<" "<<k[1].y;

}

|