题解 | #合并表记录#

合并表记录

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")

全部评论

相关推荐

迷茫的大四🐶:好一个误闯天家,我也想闯一闯
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务