PAT-乙-1032 1032 挖掘機技術哪家強 (20 分)
阿新 • • 發佈:2018-12-13
程式碼
#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;
}