PAT-乙-1032 1032 挖掘机技术哪家强 (20 分)

在这里插入图片描述

代码

#include <iostream>
#include <map> 

using namespace std;

int main() {

	int n;
	cin>>n;
	map<int, int> m;
	int maxPos = -1;
	int max = 0;
	for(int i=0; i<n; i++){
		int tmpPos, tmpScore;
		cin>>tmpPos>>tmpScore;
		if(m[tmpPos]==0){
			m[tmpPos] = tmpScore;
		}
		else{
			m[tmpPos] += tmpScore;
		}
		if(m[tmpPos]>max){
			max = m[tmpPos];
			maxPos = tmpPos;
		}
	}
	cout<<maxPos<<" "<<max<<endl;

	return 0;
}

注解

1、map的使用:

定义:
#include <map> 
map<int, int> m;
查找:
if(m[tmpPos]==0){
    	m[tmpPos] = tmpScore;
}
else{
    	m[tmpPos] += tmpScore;
}

结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zhanggirlzhangboy/article/details/82943367