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;
}

结果

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

相关文章:

  • 2021-08-09
  • 2021-12-12
  • 2021-10-16
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2021-11-11
猜你喜欢
  • 2022-01-11
  • 2021-06-15
  • 2021-06-17
  • 2021-11-19
  • 2021-09-09
  • 2022-12-23
相关资源
相似解决方案