求助!!!!!谢谢。

P1093 [NOIP2007 普及组] 奖学金

lihaonan @ 2016-10-24 18:06:17

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
main()
{
    int m,i,j,a[150],b[150],c[150],d[150],e[150],mx;
    scanf("%d",&m);
    memset(e,0,sizeof(e));
    memset(d,0,sizeof(d));
    for(i=0;i<m;i++)
    {
        scanf("%d%d%d",&a[i],&b[i],&c[i]);
        d[i]=a[i]+b[i]+c[i];e[i]=i+1;
    }
    for(i=0;i<m;i++)
     {
     for(j=0;j<m-i;j++)
     {
     if(d[j]<d[j+1])
      {mx=d[j];d[j]=d[j+1];d[j+1]=mx;mx=e[j];e[j]=e[j+1];e[j+1]=mx;}
      }
      }
    for(i=0;i<m;i++)
    {
    if(d[i]==d[i+1])
     {if(a[i]<a[i+1])
     {mx=e[i];e[i]=e[i+1];e[i+1]=mx;}}
     if((d[i]==d[i+1])&&(a[i]==a[i+1]))
     {if(e[i]>e[i+1])
     {mx=e[i];e[i]=e[i+1];e[i+1]=mx;}}
     }
     for(i=0;i<=4;i++)
     {printf("%d %d",e[i],d[i]);printf("\n");}
     getchar();getchar();return 0;
}

by hellomath @ 2016-12-10 13:13:31

very easy!

#include <iostream>
#include <algorithm>
using namespace std;
typedef struct 
{
    int number;
    int chinese;
    int score;
} student;
int comp(student a, student b)
{
    if (a.score != b.score)
        return a.score > b.score;
    else if (a.chinese != b.chinese)
        return a.chinese > b.chinese;
    else
        return a.number < b.number;
}
int main()
{
    int n;
    cin>>n;
    student a[n];
    for (int i=0; i<n; i++)
    {
        a[i].number = i + 1;
        cin>>a[i].chinese;
        int math, english;
        cin>>math>>english;
        a[i].score = a[i].chinese + math + english;
    }
    sort(a, a+n, comp);
    for (int i=0; i<5; i++)
        cout<<a[i].number<<" "<<a[i].score<<endl;
    return 0;
}

|