题目:《扑克牌游戏》
代码如下:
#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string, pair<int, int> > mp;
int main() {
int T, n, grade;
string name;
cin >> T;
while (T--) {
mp.clear();
cin >> n;
for (int i = 0; i < n; i++) {
cin >> name >> grade;
if (mp.count(name)) {
mp[name].first += grade;
mp[name].second = i;
} else {
mp[name] = make_pair(grade, i);
}
}
map< string, pair<int, int> >::iterator it;
int maxm = -2147483648, posTime = 2147483647;
string posName = "";
for (it = mp.begin(); it != mp.end(); it++) {
if (it->second.first > maxm) {
maxm = it->second.first;
posTime = it->second.second;
posName = it->first;
} else if (it->second.first == maxm) {
if (it->second.second < posTime) {
posTime = it->second.second;
posName = it->first;
}
}
}
cout << posName << endl;
}
return 0;
}
|