当前位置:网站首页>HJ8 merge table records
HJ8 merge table records
2022-08-07 14:03:00 【Hash hip-hop】
Description
Data table records include table index index and numerical value (positive integer in the int range). Please merge the records with the same table index, that is, sum the values of the same index, and output the output in ascending order of the index value.
Hint:
0 <= index <= 11111111
1 <= value <= 100000
Enter description:
First enter the number of key-value pairs n (1 <= n <= 500)
Next n lines enter paired index and value values in each line, separated by spaces
Output description:
Output the merged key-value pair (multiple lines)
Example 1
Enter:
4
0 1
0 2
1 2
3 4
Output:
0 3
1 2
3 4
Analysis
- Create an empty object to receive key-value pairs
- Cut the incoming string by spaces and turn it into an array
- Use k and v to store two items of data in the array respectively k = arr[0] v = arr[1]
- Judging whether the key names of the arrays are the same, if they are the same, the values are added, and if they are different, the corresponding v is stored
Code
let obj = {}while((str = readline())){const arr = str.split(' ')const k = +arr[0]const v = +arr[1]if(v) {// If the key name is repeated, the key value is addedif(obj[k]) {obj[k] += v} else {// If there is no same key name, store this key valueobj[k] = v}}}// Traverse the obj object and output the value of the key-value pairfor(key in obj) {console.log(key + ' ' + obj[key])}边栏推荐
猜你喜欢

1.0 Common commands and permission understanding

Error: required args <xml=string> at error (index.esm.js?93ce:68:1) at Parser.parse (index.e

Event registration: How to efficiently respond to the current real-time scene needs?

Navicat连接mysql一段时间未操作之后卡住几秒

学习笔记:时间复杂度和空间复杂度

SwiftUI任意继承层级中视图被裁剪显示不全的解决方案

传统网站与数字化网站的对比

After using the vxe-table row merge, there will be an extra column blank after the merged row.

_ dynamic programming code hakuouki zuisouroku note cash II _518 change

ENScanGo主域名批量提取脚本
随机推荐
Tapdata 开源项目基础教程:功能特性及实操演示
5. Chinese version of cuBLAS Development Guide--Create() and Destroy() in cuBLAS
网页设计(二)
Multiple data source configuration
使用OpenCV测量图像中物体的大小
USB Module Analysis (4) - Device List & Permission Application
postgresql逻辑备份工具pg_dump和pg_resotre学习
对单张图像循环进行多次超分辨,图像增强,去模糊等图像处理是否合理?以及如何评价图像质量?
Realize database addition, deletion, modification and query in one page
LeetCode_6141_合并相似的物品
Weekend resumption, masters do not sell products but tell stories!
Postgresql logical backup tools pg_dump and pg_resotre learning
LeetCode high-frequency question: longest common subsequence, playing game A and game B, how many prizes can the two brothers get by adding up
7.cuBLAS开发指南中文版--cuBLAS中的cublasSetVector()和cublasGetVector()
X64汇编语言指令编码
活动报名:如何高效应对当下的实时场景需求?
Campus basketball web finished goods movement series of NBA basketball theme school basketball template pages simple sports website design product
[Leetcode]21. 合并两个有序链表
tensorRT(一)| tensorRT如何进行推理加速?
C语言标准输入输出(11)