当前位置:网站首页>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;
}
边栏推荐
- Overview of wechat public platform development
- 多线程环境下CountDownLatch的用法
- Inside router of network equipment hard core technology (10) disassembly of Cisco asr9900 (4)
- 积分运算电路的设计方法详细介绍
- lua学习笔记
- The design method of integral operation circuit is introduced in detail
- Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
- 《剑指Offer》剪绳子
- Leetcode 456.132 mode monotone stack /medium
- DIY制作示波器的超详细教程:(一)我不是为了做一个示波器
猜你喜欢

Leetcode 190. reverse binary bit operation /easy

3.3-5v转换

The design method of integral operation circuit is introduced in detail

reflex

MySQL 面试40连问,面试官你再问下去我可要翻脸了

华为鸿蒙模拟器去除顶部导航栏方法

After configuring corswebfilter in grain mall, an error is reported: resource sharing error:multiplealloworiginvalues

西瓜书《机器学习》阅读笔记之第一章绪论

仅做两项修改,苹果就让StyleGANv2获得了3D生成能力

Leetcode 781. rabbit hash table in forest / mathematical problem medium
随机推荐
网络设备硬核技术内幕 路由器篇 5 汤普金森漫游网络世界(上)
Notice on printing and distributing the Interim Measures for the administration of green manufacturing pilot demonstration of Shenzhen Bureau of industry and information technology
Network equipment hard core technology insider router Chapter 6 tompkinson roaming the online world (middle)
LeetCode 190. 颠倒二进制位 位运算/easy
仅做两项修改,苹果就让StyleGANv2获得了3D生成能力
AssetBundle如何打包
MySQL interview 40 consecutive questions, interviewer, if you continue to ask, I will turn my face
初探STM32掉电复位PDR
STM32 CAN 通信 滤波设置问题
Google team launches new transformer to optimize panoramic segmentation scheme CVPR 2022
Internship: compilation of other configuration classes
Principle of MOS tube to prevent reverse connection of power supply
Leetcode-1737-满足三条件之一需改变的最少字符数
Deveco studio2.1 operation item error
反射
Problem solving in magic tower project
Method of removing top navigation bar in Huawei Hongmeng simulator
EMC design scheme of CAN bus
积分运算电路的设计方法详细介绍
Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)