90分求调

P1093 [NOIP2007 普及组] 奖学金

ykhk10710o_o @ 2024-05-04 12:39:16

#include<bits/stdc++.h>
#define int long long 
using namespace std;
struct man{
    int yu,zong,xue;
}xs[1011];
signed main(){
    int n,a,b;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>xs[i].yu>>a>>b;
        xs[i].zong=xs[i].yu+a+b;
        xs[i].xue=i;
    }
    for(int i=1;i<=n+911;i++){
        for(int j=1;j<=n;j++){
            if(xs[j].zong<xs[j+1].zong){
                swap(xs[j].zong,xs[j+1].zong);
                swap(xs[j].xue,xs[j+1].xue);
            }else{
                if(xs[j].zong==xs[j+1].zong&&xs[j].yu<xs[j+1].yu){
                    swap(xs[j].zong,xs[j+1].zong);
                    swap(xs[j].xue,xs[j+1].xue);
                }else{
                    if(xs[j].zong==xs[j+1].zong&&xs[j].yu==xs[j+1].yu&&xs[j].xue>xs[j+1].xue){
                        swap(xs[j].zong,xs[j+1].zong);
                        swap(xs[j].xue,xs[j+1].xue);
                    }
                }
            }
        } 
    } 
    for(int i=1;i<=5;i++){
             cout<<xs[i].xue<<" "<<xs[i].zong<<endl;
    } 
    return 0;
}

by Chu_awa_xing @ 2024-05-12 17:19:26

不用写这么麻烦,学一学结构体排序再写这道题


by zhuyucheng6046 @ 2024-06-01 19:30:03

正确代码:


#include<bits/stdc++.h>
using namespace std;
struct stu
{
    int chinese,math,english,zongfen,xuehao;
}student[1000];
bool cmp(stu a,stu b)
{
    if(a.zongfen>b.zongfen)return 1;
    else if(a.zongfen<b.zongfen)return 0;
    else
    {
        if(a.chinese>b.chinese)return 1;
        else if(a.chinese<b.chinese)return 0;
        else
        {
            if(a.xuehao>b.xuehao)return 0;
            else
            return 1;
        }
    }
}
int main()
{
    int n,s=1;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>student[i].chinese >>student[i].math >>student[i].english ;
        student[i].xuehao =s;s++;
        student[i].zongfen =student[i].chinese+student[i].math+student[i].english;
    }
    sort(student,student+n,cmp );
    for(int i=0;i<5;i++)
    {
        cout<<student[i].xuehao<<" "<<student[i].zongfen <<endl;
    }
    return 0;
}

|