题解80,我是90,怎么AC

P1093 [NOIP2007 普及组] 奖学金

Lying_Flat666 @ 2022-08-04 19:41:47

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 320;
int n;
struct whr
{
    int num,sum,yu,shu,wai;
}a[N];
bool cmp(whr p,whr q)
{
    if (p.sum!=q.sum) return p.sum>q.sum;
    if (p.sum==q.sum)
    {
        if (p.yu!=q.yu)
        return p.yu>q.yu;
    }
    if (p.sum==q.sum&&p.yu==q.yu) return p.num>q.num;
}
inline int read()
{
    int x=0,w=1; char c=getchar();
    while (c>'9'||c<'0') {if (c=='-') w=-1; c=getchar();}
    while (c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
    return w*x;
}
int main()
{
    n=read();
    for (int i=1;i<=n;++i)
    {
        a[i].num=i;
        a[i].yu=read(); a[i].shu=read(); a[i].wai=read();
        a[i].sum=a[i].yu+a[i].shu+a[i].wai;
    }
    sort(a+1,a+n+1,cmp);
    for (int i=1;i<=5;++i)
    printf("%d %d\n",a[i].num,a[i].sum);
    return 0; 
}

by zhenjianuo2025 @ 2022-08-04 19:44:29

我是 100


by JustinXiaoJunyang @ 2022-08-04 19:48:25

@Lying_Flat666 求关注~

#include <iostream>
#include <algorithm>
using namespace std;
struct Person
{
    int id, chi, math, eng, sum;
    void read()
    {
        cin >> chi >> math >> eng;
        sum = chi + math + eng;
    }
    void write()
    {
        cout << id << " " << sum << endl;
    }//要在结构体内定义一个输出数组
};
Person a[310];
bool cmp(Person x, Person y)
{
    if (x.sum != y.sum)
    {
        return x.sum > y.sum;
    }
    if (x.chi != y.chi)
    {
        return x.chi > y.chi;
    }
    return x.id < y.id;
}
int main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        a[i].read();
        a[i].id = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= 5; i++)
    {
        a[i].write();
    }
    return 0;
}

by pig2014 @ 2022-11-21 20:10:48

@Lying_Flat666 num的顺序关系写反了


|