70分求助!

P1093 [NOIP2007 普及组] 奖学金

qzhw @ 2021-09-19 10:02:48

#include<bits/stdc++.h>
using namespace std;
int n;
struct node
{
    int bh;
    int chinese;
    int maths;
    int english;
    int sum;
}a[305];
bool cmp(node x,node y)
{
    return x.sum>y.sum;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i].chinese>>a[i].maths>>a[i].english;
    for(int i=1;i<=n;i++)
    a[i].sum=a[i].chinese+a[i].maths+a[i].english;
    for(int i=1;i<=n;i++)
    a[i].bh=i;
    stable_sort(a+1,a+1+n,cmp);
    for(int i=1;i<=5;i++)
    {
        if(a[i].sum==a[i+1].sum)
        {
            if(a[i].chinese>a[i+1].chinese)
            swap(a[i].sum,a[i].sum);

        }
        cout<<a[i].bh<<" "<<a[i].sum<<"\n";
    }
    return 0;
}

by Waaifu_D @ 2021-09-19 10:04:10

如果两个同学总分相同,再按语文成绩从高到低排序,如果两个同学总分和语文成绩都相同,那么规定学号小的同学 排在前面,这样,每个学生的排序是唯一确定的。


by Waaifu_D @ 2021-09-19 10:04:29

@qzhw 建议读题


by qzhw @ 2021-09-19 10:19:18

谢谢大佬点拨


by qzhw @ 2021-09-19 10:20:44

@Waaifu_D

但这样还是70分呀

#include<bits/stdc++.h>
using namespace std;
int n;
struct node
{
    int bh;
    int chinese;
    int maths;
    int english;
    int sum;
}a[305];
bool cmp(node x,node y)
{
    return x.sum>y.sum;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i].chinese>>a[i].maths>>a[i].english;
    for(int i=1;i<=n;i++)
    a[i].sum=a[i].chinese+a[i].maths+a[i].english;
    for(int i=1;i<=n;i++)
    a[i].bh=i;
    stable_sort(a+1,a+1+n,cmp);
    for(int i=1;i<=5;i++)
    {
        if(a[i].sum==a[i+1].sum)
        {
            if(a[i].chinese<a[i+1].chinese)
            swap(a[i].sum,a[i+1].sum);
        }
        cout<<a[i].bh<<" "<<a[i].sum<<"\n";
    }
    return 0;
}

by qzhwDGS @ 2021-09-19 10:22:06

都快初赛了,你**还在刷题???


by Waaifu_D @ 2021-09-19 10:24:57

@qzhwDGS 你说这话是什么意思...提高自己的代码能力对初赛很有帮助的,你一个劲刷初赛题没有代码能力也就只能做做前面的选择题了,后面的阅读程序和补全程序你不要了吗?


by wkh2008 @ 2021-09-19 10:25:14

刚改好的热气腾腾的AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
struct node
{
    int bh;
    int chinese;
    int maths;
    int english;
    int sum;
}a[305];
bool cmp(node x,node y)
{
    if(x.sum>y.sum){
        return true;
    }else if(x.sum==y.sum){
        if(x.chinese>y.chinese){
            return true;
        }else if(x.chinese==y.chinese){
            return x.bh<y.bh;
        }
    }
    return false;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i].chinese>>a[i].maths>>a[i].english;
        a[i].sum=a[i].chinese+a[i].maths+a[i].english;
        a[i].bh=i;
    }

    stable_sort(a+1,a+1+n,cmp);

    for(int i=1;i<=5;i++)
    {
        cout<<a[i].bh<<" "<<a[i].sum<<"\n";
    }
    return 0;
}

by qzhwDGS @ 2021-09-19 10:25:44

/*Don't be silly,boy!*/
#include<bits/stdc++.h>
using namespace std;
long long n,k,m,t=0;
struct hj
{
    int c,m,e;
    int num=0;
    int z;
}a[200002];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i].c);
        scanf("%d",&a[i].m);
        scanf("%d",&a[i].e);
        a[i].num=i;
        a[i].z=a[i].c+a[i].m+a[i].e ;
    }
                        //冒泡多香
    for(int i=n-1;i>=1;i--)
    {
        for(int j=1;j<=i;j++)
        {
            if(a[j+1].z>a[j].z) swap(a[j],a[j+1]);
            if(a[j+1].z==a[j].z)
            {
                if(a[j+1].c>a[j].c) swap(a[j],a[j+1]);
            }
        }
    }
    for(int i=1;i<=5;i++)
    {
        printf("%d ",a[i].num);
        printf("%d\n",a[i].z);
    }
}

by Waaifu_D @ 2021-09-19 10:27:22

@qzhw 改成这样就可以了,输出的时候再判断也许会被卡。。。

#include<bits/stdc++.h>
using namespace std;
int n;
struct node
{
    int bh;
    int chinese;
    int maths;
    int english;
    int sum;
}a[305];
bool cmp(node x,node y)
{
    if(x.sum!=y.sum)return x.sum>y.sum;
    else{
        if(x.chinese!=y.chinese) return x.chinese>y.chinese;
        else return x.bh<y.bh;
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i].chinese>>a[i].maths>>a[i].english;
    for(int i=1;i<=n;i++)
    a[i].sum=a[i].chinese+a[i].maths+a[i].english;
    for(int i=1;i<=n;i++)
    a[i].bh=i;
    stable_sort(a+1,a+1+n,cmp);
    for(int i=1;i<=5;i++)
    {
        cout<<a[i].bh<<" "<<a[i].sum<<"\n";
    }
    return 0;
}

by qzhw @ 2021-09-19 10:29:06

你们不要吵了 。。。

那个骂我的其实是我同学。。。


| 下一页