70分求助

P1093 [NOIP2007 普及组] 奖学金

ylzxbcyy @ 2023-11-01 10:22:52

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{struct Chengji{int xh;
    int zcj;
}chengji[301];
int rs;
cin>>rs;
for(int i=1;i<=rs;i++)
{int y,s,w;
cin>>y>>s>>w;
int sum=y+s+w;
chengji[i]={i,sum};}
for(int i=1;i<rs;i++)
{for(int o=i+1;o<=rs;o++)
{if(chengji[o].zcj>chengji[i].zcj)
{int t=chengji[o].zcj;
chengji[o].zcj=chengji[i].zcj;
chengji[i].zcj=t;
int s=chengji[o].xh;
chengji[o].xh=chengji[i].xh;
chengji[i].xh=s;
}else if(chengji[o].zcj==chengji[i].zcj&&chengji[i].xh>chengji[o].xh)
 {int z=chengji[o].xh;
 chengji[o].xh=chengji[i].xh;
 chengji[i].xh=z;
 }
}
}
for(int i=1;i<=5;i++)
{cout<<chengji[i].xh<<" "<<chengji[i].zcj<<endl;
}

}

by Administrator2023 @ 2024-03-13 17:21:34

ac代码您看看,但愿能帮到

#include <bits/stdc++.h>
using namespace std;
const int M = 3e2 + 10;

int n;
struct NODE {
    int id, chi, math, eng, sum;
} infor[M];

bool cmp(NODE x, NODE y) {
    if(x.sum == y.sum) {
        if(x.chi != y.chi) return x.chi > y.chi;
        else return x.id < y.id;
    }
    return x.sum > y.sum;
}
int main() {
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> infor[i].chi >> infor[i].math >> infor[i].eng;
        infor[i].id = i + 1;
        infor[i].sum = infor[i].chi + infor[i].math + infor[i].eng;
    }
    sort(infor, infor + n, cmp);
    for(int i = 0; i < 5; ++i) {
        cout << infor[i].id << " " << infor[i].sum << endl;
    }
    return 0;
}

|