最后一个点过不去,求助

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

ltx2007 @ 2020-08-07 08:32:55

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    string name[n+1]={};
    int c[n+1]={},m[n+1]={},e[n+1]={},t[n+1]={};
    for(int i=0;i<n;i++){
        cin>>name[i]>>c[i]>>m[i]>>e[i];
        t[i]=c[i]+m[i]+e[i];
    }
    for(int i=n-1;i>=1;i--){
        for(int j=0;j<i;j++){
            if(t[j]>t[j+1]){
                swap(c[j],c[j+1]);
                swap(m[j],m[j+1]);
                swap(e[j],e[j+1]);
                swap(name[j],name[j+1]);
            }
        }
    }
    cout<<name[0]<<" "<<c[0]<<" "<<m[0]<<" "<<e[0]; 
//    for(int i=0;i<n;i++){
//      cout<<name[i]<<" "<<c[i]<<" "<<m[i]<<" "<<e[i]<<endl; 
//  }
    return 0;
}

谢谢大佬来解答


by jyb666 @ 2020-08-07 08:37:16

@ltx2007 结构体+sort不香嘛


by HECZ @ 2020-08-07 08:38:47

好像是排序写的有问题吧,我从没见过这样排序的?


by HECZ @ 2020-08-07 08:39:14

另外,这题正解是用结构体


by ltx2007 @ 2020-08-07 08:41:54

@HECZ 我按冒泡排序写的啊???


by jyb666 @ 2020-08-07 08:42:25

@ltx2007 sort快拍多香QwQ


by jyb666 @ 2020-08-07 08:42:58

@ltx2007 稍等,我码下代码


by jyb666 @ 2020-08-07 08:54:14

#include <bits/stdc++.h>
using namespace std;
struct abc
{
    string name;
    int c,m,e,t;
}a[1010];
bool cmp(abc x,abc y)
{
    return x.t>y.t;
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].name>>a[i].c>>a[i].m>>a[i].e;
        a[i].t=a[i].c+a[i].m+a[i].e;
    }
    sort(a,a+n,cmp);
    cout<<a[0].name<<" "<<a[0].c<<" "<<a[0].m<<" "<<a[0].e;
    return 0;
}

by jyb666 @ 2020-08-07 08:54:45

@ltx2007 新鲜出炉,你看看


by ltx2007 @ 2020-08-07 08:59:07

@jyb666 谢谢大佬


|