1. 程式人生 > >hdu 1236 排名,大神教過寫出來的,SORT

hdu 1236 排名,大神教過寫出來的,SORT

# include<algorithm>sort的標頭檔案
# include<stdio.h>
# include<string.h>
using namespace std;
struct news{
               char kaohao[22];
                int sum;
           }ks[1010];
bool cmp(news a,news b)
{
     if(a.sum!=b.sum)
           return a.sum>b.sum;
     else return strcmp(a.kaohao,b.kaohao)<0;//比較考號
}
int main()
{//定義的變數較多,一定要知道變數的用途,以免混淆
     int N,M,G,i,j,add,num,th,k;
     int fenzhi[12];char kh[22];
     while(scanf("%d",&N),N)
     {
       memset(ks,0,sizeof(ks));
       k=0;
        scanf("%d%d",&M,&G);
        for(i=1;i<=M;i++)
          scanf("%d",&fenzhi[i]);
        for(i=1;i<=N;i++)//迴圈n次,無論如何變
        {
             add=0;
             getchar();
             scanf("%s",kh);
             scanf("%d",&num);
             for(j=1;j<=num;j++)
              {
                      scanf("%d",&th);//第一次錯誤居然是把為了省事,把這用i,導致全錯誤
                      add+=fenzhi[th];
              }
              if(add>=G) {
                             strcpy(ks[k].kaohao,kh);//把符合的放到結構體中
                             ks[k].sum=add;
                             k++;
                         }                
        }
             sort(ks,ks+k,cmp);
               printf("%d\n",k);
               for(i=0;i<k;i++)
                 printf("%s %d\n",ks[i].kaohao,ks[i].sum);
                           
     }
     return 0;
}