HUAWEI Written Questions: Consolidated Table Record

Title description

The data table record contains the table index and the value (integer of int range). Please merge the records with the same table index. That is, the values ​​of the same index are summed, and the output is output in ascending order of key value.

Enter description:

Enter the number of key-value pairs first,
then enter the index and value pairs, separated by spaces

Output description:

Output the combined key-value pair (multiple lines)

Example 1

Input

4
0 1
0 2
1 2
3 4

Output

0 3
1 2
3 4
#include <iostream>
#include <map>

using namespace std;

int main() {
    int n;
    cin >> n;
    map<int, int> m;
    for (int i = 0; i < n; ++i) {
        int index, value;
        cin >> index >> value;
        m[index] += value;
    }
    map<int, int>::iterator it;
    for (it = m.begin(); it != m.end(); it++) {
        cout << it->first << " " << it->second << endl;
    }
    return 0;
}

 

Published 34 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_41111088/article/details/104789130