当前位置:网站首页>HJ8 合并表记录
HJ8 合并表记录
2022-07-27 14:23:00 【coder_Alger】
描述
数据表记录包含表索引和数值(int 范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照 key 值升序进行输出。
输入描述:
先输入键值对的个数
然后输入成对的 index 和 value 值,以空格隔开
输出描述:
输出合并后的键值对(多行)
示例 1
输入:
4 0 1 0 2 1 2 3 4
输出:
0 3 1 2 3 4
#include <iostream>
#include <map>
using namespace std;
int main()
{
int n;
map<int,int>m;
cin>>n;//输入键值对的个数
for(int i = 0;i<n;i++)
{
pair<int,int>tmp;
cin>>tmp.first;
cin>>tmp.second;
if(m.find(tmp.first)!= m.end())
{
//m[tmp.first] += tmp.second
m[tmp.first] += tmp.second;
}
else
{
m[tmp.first] = tmp.second;
}
}
for(map<int,int>::iterator it = m.begin();it != m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
边栏推荐
- Network equipment hard core technology insider router Chapter 6 tompkinson roaming the online world (middle)
- 分布式锁
- 4种单片机驱动继电器方案
- 《剑指Offer》两个链表的第一个公共结点
- Network equipment hard core technology insider router Chapter 13 from deer by device to router (Part 1)
- DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
- Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
- USB interface electromagnetic compatibility (EMC) solution
- JUC(JMM、Volatile)
- Distributed lock
猜你喜欢
随机推荐
两阶段提交与三阶段提交
DevEco Studio2.1运行项目报错
2022-07-27日报:IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一
网络设备硬核技术内幕 路由器篇 7 汤普金森漫游网络世界(下)
仅做两项修改,苹果就让StyleGANv2获得了3D生成能力
《终身成长》读书笔记(一)
JMeter recording interface automation
Is it safe to open an account on a mobile phone?
3D相关的简单数学知识
"Sword finger offer" linked list inversion
泛型
How to package AssetBundle
Unity mouse controls the first person camera perspective
Two stage submission and three stage submission
AssetBundle如何打包
Network equipment hard core technology insider router Chapter 10 Cisco asr9900 disassembly (III)
STM32之CAN ---CAN ID过滤器分析
Introduction of the connecting circuit between ad7606 and stm32
TCC
Inside router of network equipment hard core technology (10) disassembly of Cisco asr9900 (4)








