原题链接:蓝桥杯算法提高VIP-班级排名
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<iterator>
using namespace std;
typedef pair<string,int> P;
int cmp(const P& x,const P& y){
return x.second>y.second;
}
int main(){
map<string,int> student; //存放学生。
map<string,int>::iterator it;
vector<P> v; //作为按值排序的工具
int n,m,grade;
string s,name;
cin>>n;
for(int i=0;i<n;i++) {cin>>s;student.insert(make_pair(s,0));}
cin>>m;
while(m--){
int flag=0;
for(int i=0;i<n;i++){
cin>>grade>>name;
student[name] += grade;
}
for(it=student.begin();it!=student.end();it++){
v.push_back(make_pair(it->first,it->second));
sort(v.begin(),v.end(),cmp);
}
vector<P>::iterator it_v;
int i,j;
for(i=0;i<v.size();i++){
if(v[i].first=="DaDa"){
j=i;break;
}
}
for(i=0;i<j;i++){
if(v[i].second==v[j].second){
cout<<i+1<<endl;
flag=1;
break;
}
}
if(!flag)
cout<<j+1<<endl;
v.clear();
}
}解题思路:
思路:就是用map存储,学生名作为键,学生成绩作为值,然后借用vector进行对学生的成绩进行排序。
排序后查找DaDa的学生的下标index(从0开始),然后再查找0到index中的与DaDa成绩一样的就返回其下标+1;进行下一次考试,记住此时成绩要加到map的值中,就是对应的student[name]+=grade;
记住:vector要清零,最外层while循环一次就清空一次。
注意事项:
参考代码:
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复