- #include <iostream>
- #include <cstring>
- #define SAFEDELETE_ARRAY(p) /
- { if(p) {delete []p; p=NULL; } }
- using namespace std;
- //-------------
- typedef struct student
- {
- int score;
- char name[32];
- }*Student;
- int compare(const void *p1,const void *p2)
- {
- student u1=*(student *)p1;
- student u2=*(student *)p2;
- if(u1.score>u2.score)
- return 1;
- else if(u1.score==u2.score && strcmp(u1.name,"Li Ming")==0)
- return 1;
- else
- return -1;
- }
- int cmp(const void *p1,const void *p2)
- {
- student u1=*(student *)p1;
- student u2=*(student *)p2;
- if(strcmp(u1.name,u2.name)<0)
- return 1;
- return -1;
- }
- int BiSeach(char *name,Student stu,int N)
- {
- int low=0,high=N-1;
- int mid;
- int value;
- while(low<=high)
- {
- mid=(high+low)/2;
- value=strcmp(stu[mid].name,name);
- if(!value)
- return mid;
- else if(value>0)
- low=mid+1;
- else
- high=mid-1;
- }
- }
- int main()
- {
- int N;
- cin >> N;
- getchar();
- Student stu=new student[N];
- for(int i=0;i<N;i++)
- stu[i].score=0;
- for(int i=0;i<N;i++)
- gets(stu[i].name);
- int M;
- cin >> M;
- getchar();
- int score;
- int p;
- char name[32];
- for(int i=0;i<M;i++)
- {
- qsort(stu,N,sizeof(student),cmp);
- for(int j=0;j<N;j++)
- {
- cin >> score;
- getchar();
- gets(name);
- p=BiSeach(name,stu,N);
- stu[p].score+=score;
- }
- /*for(int j=0;j<N;j++)
- {
- cout << stu[j].score << " ";
- puts(stu[j].name);
- }
- cout << endl;*/
- qsort(stu,N,sizeof(student),compare);
- /*for(int j=0;j<N;j++)
- {
- cout << stu[j].score << " ";
- puts(stu[j].name);
- }*/
- for(int k=N-1;k>=0;k--)
- if(strcmp(stu[k].name,"Li Ming")==0)
- {
- cout << N-k << endl;
- break;
- }
- }
- getchar();
- getchar();
- SAFEDELETE_ARRAY(stu);
- return 0;
- }
考察:
1. 快排
2.二分查找
特别注意:
1.字符串的输入处理
1).输入数字后的getchar()
2).在同一行既输入数字又输入字符串
2. The rank is decided by the total scores.