六,八点过不去

P1093 [NOIP2007 普及组] 奖学金

dog_of_mayi_is_me @ 2022-04-03 16:34:43

6,8点过不去


#include<bits/stdc++.h>
using namespace std;
struct student{
    int H,C,M,E,Z;//学号,语,数,英,总分 
};
student a[305];
int main()
{
    int n,num;
    cin>>n;
    for(int i=1;i<=n;i++)
        {
            cin>>a[i].C;cin>>a[i].M;cin>>a[i].E;
            a[i].Z=a[i].C+a[i].M+a[i].E;
            a[i].H=i;
        }   //输入
    for(int i=1;i<=n;i++)
    {
        //每次循环完结果为将:最大移到最后(即n-i个),其余往前移一个
        for(int j=1;j<=n-i;j++)
        {   if(a[j].Z<a[j+1].Z)
            {
                num=a[j+1].Z;
                a[j+1].Z=a[j].Z;
                a[j].Z=num;
                num=a[j+1].H;
                a[j+1].H=a[j].H;
                a[j].H=num;
            }
            if(a[j].Z==a[j+1].Z)
            {
                if(a[j].C<a[j+1].C)
                {
                    num=a[j+1].Z;
                    a[j+1].Z=a[j].Z;
                    a[j].Z=num;
                    num=a[j+1].H;
                    a[j+1].H=a[j].H;
                    a[j].H=num;
                }
                if(a[j].C==a[j+1].C)
                {
                    if(a[j].H>a[j+1].H)
                    {
                    num=a[j+1].H;
                    a[j+1].H=a[j].H;
                    a[j].H=num;
                    }
                }
            }

        } 
    }
        //每次循环完结果为将:最大移到最后(即n-i个),其余往前移一个
    for(int i=1;i<=5;i++)
        cout<<a[i].H<<" "<<a[i].Z<<endl;
}

by zhuzl009 @ 2022-08-07 10:52:57

一般不自己手写排序 直接用sort就行

#include <iostream>
#include <algorithm>

using namespace std;
int n;

struct p{
    int c;
    int sum;
    int id;
}stu[305];

bool cmd(p x, p y) {
    if (x.sum != y.sum) {
        return (x.sum > y.sum);
    } else {
        if (x.c != y.c) {
            return (x.c > y.c);
        } else {
            return x.id < y.id;
        }
    }
}

int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) {
        stu[i].id = i + 1;
        int chi, mat, eng;
        cin >> chi >> mat >> eng;

        stu[i].c = chi;
        stu[i].sum = chi + mat + eng;
    }

    sort(stu, stu + n, cmd);

    for(int i = 0; i < 5; i ++)
        cout << stu[i].id << " " << stu[i].sum << endl;
    return 0;
}

|