题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
#include <iostream>
using namespace std;
#include <vector>
#include <sstream>
#include <algorithm>
#include <map>
int main() {
vector<pair<int, int>> vecInput;
string strInput;
getline(cin, strInput);
int nLines = stoi(strInput);
while (getline(cin, strInput)) {
if (strInput != "") {
char chSigned = ' ';
string strTokens = "";
vector<int> vecTokens;
istringstream stringstream(strInput);
while (getline(stringstream, strTokens, chSigned)) {
vecTokens.push_back(stoi(strTokens));
}
vecInput.push_back({ vecTokens[0], vecTokens[1] });
}
}
vector<int> vecIndex;
for (pair<int, int> pairVal : vecInput) {
vecIndex.push_back(pairVal.first);
}
sort(vecIndex.begin(), vecIndex.end());
vecIndex.erase(unique(vecIndex.begin(), vecIndex.end()), vecIndex.end());
map<int, int> mapIndexwithValue;
for (pair<int, int> pairVal : vecInput) {
mapIndexwithValue[pairVal.first] += pairVal.second;
}
for (int nVal : vecIndex) {
//string strTmp= tonVal + " " + mapIndexwithValue[nVal];
cout << nVal<< " " <<mapIndexwithValue[nVal] << endl;
}
}
// 64 位输出请用 printf("%lld")
查看13道真题和解析