數(shù)據(jù)表記錄包含表索引和數(shù)值,請對表索引相同的記錄進(jìn)行合并,即將相同索引的數(shù)值進(jìn)行求和運算,輸出按照key值升序進(jìn)行輸出。
先輸入鍵值對的個數(shù)然后輸入成對的index和value值,以空格隔開
輸出描述:
輸出合并后的鍵值對(多行)
輸入例子:
40 10 21 23 4輸出例子:
0 31 23 4#include<iostream>#include<map>using namespace std; int main(){ int n; while(cin >> n){ map<int,int> m; while(n--){ int key,value; cin >> key >> value; if(!m[key]){ m[key] = value; } else m[key] += value;//不存在時賦值,存在時累加 } //map內(nèi)部本身就是按照key的大小順序進(jìn)行存儲的 for(map<int,int>::iterator it=m.begin();it!=m.end();++it){ cout << it->first << " "<< it->second << endl; } } return 0;}
新聞熱點
疑難解答