PAT_1012. The Best Rank
来源:互联网 发布:mac outlook 导入规则 编辑:程序博客网 时间:2024/06/06 14:04
//// main.cpp// PAT_1012. The Best Rank//// Created by wjq on 17/4/1.// Copyright © 2017年 wjq. All rights reserved.//#include <iostream>#include <map>#include <algorithm>#include <string.h>using namespacestd;char course[4]={'C','M','E','A'};int M,N;struct student{ string id; int grade[4]; //存放学生的成绩 C M E A int rank[5]; //存放学生的成绩排名 C M E A bestrank char symbol; //}stu[2005];map <string,student> mymap;bool cmpC(student a ,student b){ return a.grade[0]>b.grade[0];}bool cmpM(student a,student b){ return a.grade[1]>b.grade[1];}bool cmpE(student a,student b){ return a.grade[2]>b.grade[2];}bool cmpA(student a,student b){ return a.grade[3]>b.grade[3];}int ave(double a,double b,double c){ double result = (a+b+c)/3; if(result-(int)result<0.5) return (int)result; return (int)result+1;}void getBest(int num){ int curRank=1; int curGrade=stu[0].grade[num]; //curGrade表示当前最小分数 for(int i=0;i<M;i++) { if(stu[i].grade[num]==curGrade) stu[i].rank[num]=curRank; elseif(stu[i].grade[num]<curGrade) //只有当目前的学生的分数比curGrade低的时候,curRank才会增加 { curRank=i+1; stu[i].rank[num]=curRank; curGrade=stu[i].grade[num]; } if(stu[i].rank[4]>=stu[i].rank[num]) { stu[i].rank[4]=stu[i].rank[num]; stu[i].symbol=course[num]; } }}void output(){ for(int i=0;i<M;i++) cout<<stu[i].id<<" "<<stu[i].grade[0]<<" "<<stu[i].grade[1]<<" "<<stu[i].grade[2]<<" "<<stu[i].grade[3]<<endl; for(int i=0;i<M;i++) cout<<stu[i].id<<" "<<stu[i].rank[0]<<" "<<stu[i].rank[1]<<" "<<stu[i].rank[2]<<" "<<stu[i].rank[3]<<" "<<stu[i].rank[4]<<" "<<stu[i].symbol<<endl;}int main(int argc,const char * argv[]){ string temp; cin>>M>>N; for(int i=0;i<M;i++) { cin>>stu[i].id>>stu[i].grade[0]>>stu[i].grade[1]>>stu[i].grade[2]; stu[i].grade[3]=ave(stu[i].grade[0],stu[i].grade[1],stu[i].grade[2]); stu[i].rank[4]=1000; //mymap[stu[i].id]=stu[i];为什么放在这里map 之后对stu更新不会再更新map中的stu了???? } sort(stu,stu+M,cmpE); getBest(2); sort(stu,stu+M,cmpM); getBest(1); sort(stu,stu+M,cmpC); getBest(0); sort(stu,stu+M,cmpA); getBest(3); for(int i=0;i<M;i++) //必须在这里建立map的映射关系 mymap[stu[i].id]=stu[i]; for(int i=0;i<N;i++) { cin>>temp; if(mymap.find(temp)==mymap.end()) cout<<"N/A"<<endl; else cout<<mymap[temp].rank[4]<<" "<<mymap[temp].symbol<<endl; } return 0;}
水题...但是被卡了N个小时,,最后求助同学发现看错题了! 优先级是A>C>E>M 我一直想当然的以为是A>M>E>C,一直调试找不出问题- -
看了网上的代码,还是找不出错误,天了个噜...
0 0
- PAT_1012: The Best Rank
- PAT_1012.The Best Rank
- PAT_1012. The Best Rank
- 1012. The Best Rank
- PAT1012. The Best Rank
- PAT1012 The Best Rank
- 1012. The Best Rank
- 1012. The Best Rank
- 1012.The Best Rank
- pat1012 The Best Rank
- 1012. The Best Rank
- 1012 the Best Rank
- 1012. The Best Rank
- 1012 The Best Rank
- The Best Rank
- The Best Rank (25)
- 1012. The Best Rank
- 1012. The Best Rank
- Java中UDP编程
- php解决中文截取乱码问题
- TensorFlow练习23: “恶作剧”
- Java的String中的subString()方法
- 用分治法求最大子序列问题,时间复杂度O(N*logN)
- PAT_1012. The Best Rank
- 链表创建之无头节点非空链表
- 102. Binary Tree Level Order Traversal (java)
- 泛型extends和super的注意点①
- JAVA异常
- 安卓逆向系列教程 4.5 糖果星星达人
- POJ3630(Trie树) PHONE LIST 算法转载
- TensorFlow练习22: 手写汉字识别
- C/C++的奇技淫巧