90分求助

P1093 [NOIP2007 普及组] 奖学金

Hiscraft @ 2023-09-17 13:08:08

#include <bits/stdc++.h> 
using namespace std; 
int const MAXN = 310; 
int n; 

struct student {
    int x,y,total;
}a[MAXN];

int cmp(student a, student b) {
    if(a.total != b.total) 
        return a.total > b.total; 
    if(a.y!=b.y)
        return a.y>b.y;
    return a.y<b.y;
}
int main() {
    cin >> n;
    for (int i=1;i<=n;i++) {
        int math, english;  
        cin>>a[i].y>>math>>english;
        a[i].total=a[i].y+math+english;
        a[i].x=i;
    }

    sort(a+1,a+n+1,cmp);
    for (int i=1;i<=5;i++) {
        cout<<a[i].x<<" "<<a[i].total<<"\n";
    }
    return 0;
}

by RJSPZ @ 2023-09-17 13:28:30

#include<bits/stdc++.h> 
using namespace std;
int n;
struct sth{
    int chinese,math,english,mark,id;
}s[114514];
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s[i].chinese>>s[i].math>>s[i].english;
        s[i].mark=s[i].chinese+s[i].math+s[i].english;
        s[i].id=i+1;
    }
    for(int i=0;i<n;i++){
        for(int j=n-1;j>=0;j--){
            if(s[i].mark<s[j].mark&&i<j){
                swap(s[i].mark,s[j].mark);
                swap(s[i].chinese,s[j].chinese);
                swap(s[i].math,s[j].math);
                swap(s[i].english,s[j].english);
                swap(s[i].id,s[j].id);
            }
            else if(s[i].mark==s[j].mark&&s[i].chinese<s[j].chinese&&i<j){
                swap(s[i].mark,s[j].mark);
                swap(s[i].chinese,s[j].chinese);
                swap(s[i].math,s[j].math);
                swap(s[i].english,s[j].english);
                swap(s[i].id,s[j].id);
            }
            else if(s[i].mark==s[j].mark&&s[i].chinese==s[j].chinese&&s[i].id>s[j].id&&i<j){
                swap(s[i].mark,s[j].mark);
                swap(s[i].chinese,s[j].chinese);
                swap(s[i].math,s[j].math);
                swap(s[i].english,s[j].english);
                swap(s[i].id,s[j].id);
            }
        }
    }
    for(int i=0;i<5;i++){
        cout<<s[i].id<<" "<<s[i].mark<<endl;
    }
    return 0;
}

|