pku 2153 Rank List

  1. #include <iostream>
  2. #include <cstring>
  3. #define SAFEDELETE_ARRAY(p)  /
  4.     { if(p) {delete []p; p=NULL; } }
  5. using namespace std;
  6. //-------------
  7. typedef struct student
  8. {
  9.     int score;
  10.     char name[32];
  11. }*Student;
  12. int compare(const void *p1,const void *p2)
  13. {
  14.     student u1=*(student *)p1;
  15.     student u2=*(student *)p2;
  16.     if(u1.score>u2.score)
  17.         return 1;
  18.     else if(u1.score==u2.score && strcmp(u1.name,"Li Ming")==0)
  19.         return 1;
  20.     else
  21.         return -1;
  22. }
  23. int cmp(const void *p1,const void *p2)
  24. {
  25.     student u1=*(student *)p1;
  26.     student u2=*(student *)p2;
  27.     if(strcmp(u1.name,u2.name)<0)
  28.         return 1;
  29.     return -1;
  30. }
  31. int BiSeach(char *name,Student stu,int N)
  32. {
  33.     int low=0,high=N-1;
  34.     int mid;
  35.     int value;
  36.     while(low<=high)
  37.     {
  38.         mid=(high+low)/2;
  39.         value=strcmp(stu[mid].name,name);
  40.         if(!value)
  41.             return mid;
  42.         else if(value>0)
  43.             low=mid+1;
  44.         else
  45.             high=mid-1;
  46.     }
  47. }
  48. int main()
  49. {
  50.     int N;
  51.     cin >> N;
  52.     getchar();
  53.     Student stu=new student[N];
  54.     for(int i=0;i<N;i++)
  55.             stu[i].score=0;
  56.     for(int i=0;i<N;i++)
  57.         gets(stu[i].name);
  58.     int M;
  59.     cin >> M;
  60.     getchar();
  61.     int score;
  62.     int p;
  63.     char name[32];
  64.     for(int i=0;i<M;i++)
  65.     {
  66.         qsort(stu,N,sizeof(student),cmp);
  67.         for(int j=0;j<N;j++)
  68.         {
  69.             cin >> score;
  70.             getchar();
  71.             
  72.             gets(name);
  73.             p=BiSeach(name,stu,N);
  74.             stu[p].score+=score;
  75.         }
  76.         /*for(int j=0;j<N;j++)
  77.         {
  78.             cout << stu[j].score << " ";
  79.             puts(stu[j].name);
  80.         }
  81.         cout << endl;*/
  82.         qsort(stu,N,sizeof(student),compare);
  83.         /*for(int j=0;j<N;j++)
  84.         {
  85.             cout << stu[j].score << " ";
  86.             puts(stu[j].name);
  87.         }*/
  88.         for(int k=N-1;k>=0;k--)
  89.             if(strcmp(stu[k].name,"Li Ming")==0)
  90.             {
  91.                 cout << N-k << endl;
  92.                 break;
  93.             }
  94.     }
  95.     getchar();
  96.     getchar();
  97.     SAFEDELETE_ARRAY(stu);
  98.     return 0;
  99. }

考察:

    1. 快排

    2.二分查找

特别注意:

    1.字符串的输入处理

    1).输入数字后的getchar()

    2).在同一行既输入数字又输入字符串

    2. The rank is decided by the total scores.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值