华为OJ_1956_合并表记录

本文介绍了一种简单的键值对合并算法,并提供了完整的C++实现代码。该算法通过使用标准模板库(STL)中的map容器来高效地完成键值对的合并操作,适用于需要处理大量键值对数据并进行合并的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

输入:  
先输入键值对的个数
然后输入成对的index和value值,以换行符隔开


输出:  
输出合并后的键值对(多行)


样例输入: 
4
3
4
0
1
0
2
1
2


                   
样例输出: 
0
3
1
2
3

4

#if 1
#include <iostream>
#include <map>

using namespace std;

int main( void )
{
	int num;
	int index, value;
	map<int,int> mapIV;
	map<int,int>::iterator it;

	cin >> num;
	while( num-- ){
		cin >> index >> value;
		it = mapIV.find( index );
		if( it == mapIV.end() )
			mapIV[index] = value;
		else
			mapIV[index] += value;
	}

	for( it = mapIV.begin(); it != mapIV.end(); ++it ){
		cout << it->first << endl;
		cout << it->second << endl;
	}

	return 0;
}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值