Pat a problem-1012. The best Rank (25)-Sort water questions

Source: Internet
Author: User

Sorting, water problems
Because finally if the best ranking of a student has the same, the output of the course has a priority a>c>m>e
Then sort by this order of precedence
Find the current course ranking before each sort
And then compare and update with the best rankings of the current

As for the query, establish the ID and index mapping.

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<map>using namespacestd;Const intmaxn= -+5;intN,m;map<string,int>maps;structstu{Charid[Ten]; intscore[4]; intranks; intbest_rank= the; intC;} STU[MAXN];BOOLCmp1 (Stu a,stu b) {returna.score[0]>b.score[0];}BOOLCMP2 (Stu a,stu b) {returna.score[1]>b.score[1];}BOOLCmp3 (Stu a,stu b) {returna.score[2]>b.score[2];}BOOLCmp4 (Stu a,stu b) {returna.score[3]>b.score[3];}voidSolveranks (intk) {stu[0].ranks=1; if(stu[0].ranks<stu[0].best_rank) {stu[0].best_rank=stu[0].ranks; stu[0].c=K; }     for(intI=1; i<n;i++){        if(stu[i].score[k]==stu[i-1].score[k]) {stu[i].ranks=stu[i-1].ranks; }        Else{stu[i].ranks=i+1; }        if(stu[i].ranks<Stu[i].best_rank) {Stu[i].best_rank=stu[i].ranks; STU[I].C=K; }    }}intMain () {scanf ("%d%d",&n,&m);  for(intI=0; i<n;i++) {scanf ("%s %d%d%d", stu[i].id,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); stu[i].score[3]= (stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3; } sort (Stu,stu+n,cmp4); Solveranks (3); Sort (Stu,stu+N,CMP1); Solveranks (0); Sort (Stu,stu+N,CMP2); Solveranks (1); Sort (Stu,stu+N,cmp3); Solveranks (2); stringstr;  for(intI=0; i<n;i++) {str=stu[i].id; MAPS[STR]=i+1; }    Charcourse[4]={'C','M','E','A'};  for(intI=0; i<m;i++) {cin>>str; intidx=maps[str]-1; if(idx==-1) printf ("n/a\n"); Elseprintf ("%d%c\n", stu[idx].best_rank,course[stu[idx].c]); }    return 0;}
View Code

Pat a problem-1012. The best Rank (25)-Sort water questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.