WAWAWAWAWA——0分求助

P1093 [NOIP2007 普及组] 奖学金

hcodm @ 2024-07-19 09:12:31

#include<bits/stdc++.h>
using namespace std;
struct xueshen{
    int chinese,shuxue,english;
    int xuehao;
}chenji[500];
int n,zongfen[500];
main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>chenji[i].chinese>>chenji[i].shuxue>>chenji[i].english;
        chenji[i].xuehao=i;
        zongfen[i]=zongfen[i]+chenji[i].chinese+chenji[i].shuxue+chenji[i].english;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(zongfen[j-1]<zongfen[j]){
                swap(zongfen[j-1],zongfen[j]);
                swap(chenji[j-1].xuehao,chenji[j].xuehao);
            }
            if(zongfen[j-1]==zongfen[j]){
                if(chenji[j-1].xuehao>chenji[j].xuehao){
                    swap(zongfen[j-1],zongfen[j]);
                    swap(chenji[j-1].xuehao,chenji[j].xuehao);
                }
            }
        }
    }
    for(int i=1;i<=5;i++){
        cout<<chenji[i].xuehao<<" "<<zongfen[i]<<endl;
    }
    return 0;
}

大佬勿喷


by Wyl20110306 @ 2024-07-19 09:32:03


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

by Wyl20110306 @ 2024-07-19 09:33:13

@hcodm 少了cmp函数且排序错误


by hcodm @ 2024-07-19 09:36:46

@Wyl20110306 @Wyl20110306 @Wyl20110306 @Wyl20110306 @Wyl20110306 @Wyl20110306 @Wyl20110306 @Wyl20110306 谢谢,已关


by Wyl20110306 @ 2024-07-19 09:37:14

@hcodm 没事


by W_R_Q @ 2024-07-20 09:55:24

#include<bits/stdc++.h>
using namespace std;
struct student{
    int xuehao;
    int zongfen;
    int yuwen;
    int shuxue;
    int yingyu;
}a[310];
int n;
int cmp(student one,student two){
    if(one.zongfen==two.zongfen){
        if(one.yuwen==two.yuwen){
            return one.xuehao<two.xuehao;
        }
        return one.yuwen>two.yuwen;
    }
    return one.zongfen>two.zongfen;
}
int main(){
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a[i].yuwen>>a[i].shuxue>>a[i].yingyu;
        a[i].zongfen=a[i].yuwen+a[i].shuxue+a[i].yingyu;
        a[i].xuehao=i+1;
    }
    sort(a,a+n,cmp); 
    for(int i=0;i<5;i++){
        cout<<a[i].xuehao<<" "<<a[i].zongfen<<endl;
    }
    return 0; 
}

|