Snow_maple


私信TA

用户名:2016114438

访问量:21043

签 名:

我喜欢一个人,她叫鞠婧祎!

等  级
排  名 251
经  验 5822
参赛次数 2
文章发表 32
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

#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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区