当前位置:网站首页>HJ8 合并表记录
HJ8 合并表记录
2022-08-07 14:00:00 【哈希嘻哈】
描述
数据表记录包含表索引index和数值value(int范围的正整数),请对表索引相同的记录进行合并,即将相同索引的数值进行求和运算,输出按照index值升序进行输出。
提示:
0 <= index <= 11111111
1 <= value <= 100000
输入描述:
先输入键值对的个数n(1 <= n <= 500)
接下来n行每行输入成对的index和value值,以空格隔开
输出描述:
输出合并后的键值对(多行)
示例1
输入:
4
0 1
0 2
1 2
3 4
输出:
0 3
1 2
3 4
分析
- 创建一个空的对象来接收键值对
- 把传入的字符串通过空格切割,变成一个数组
- 分别用k , v 来存放数组的两项数据 k = arr[0] v = arr[1]
- 判断数组的键名是否相同,相同则值相加,不同则存储相应的v
代码
let obj = {
}
while((str = readline())){
const arr = str.split(' ')
const k = +arr[0]
const v = +arr[1]
if(v) {
// 如果键名重复,则键值相加
if(obj[k]) {
obj[k] += v
} else {
// 没有相同的键名,就存储这个键值
obj[k] = v
}
}
}
// 遍历obj对象,输出键值对的值
for(key in obj) {
console.log(key + ' ' + obj[key])
}
边栏推荐
- change of mind
- LOGO 8.3 Web Server function
- 1.0 Common commands and permission understanding
- 开发者成长激励计划-开发笔记:最简步骤移植LVGL
- CSO face to face | Dialogue with mini world, talk about the safety construction of the game industry
- Weekend resumption, masters do not sell products but tell stories!
- [Unity entry plan] GameObject class (1) - GetComponent method
- NestedTensor
- 移相带来的振荡器
- postgresql逻辑备份工具pg_dump和pg_resotre学习
猜你喜欢

FairMOT Online Tracking Framework and Code Learning Records

移相带来的振荡器

rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp)

LinkedList 底层学习

keil编程问题 -- main.c(75): warning: #223-D: function “XXX“ declared implicitly

The Golden Nine Silver Ten interview season is coming, how does the Android programmer get the offer?

mysql database source command to import sql file records

xss过滤绕过笔记

LOGO 8.3 Web Server function

PDF conversion tool installation tutorial
随机推荐
怎么简单实现菜单拖拽排序的功能
Weekend resumption, masters do not sell products but tell stories!
使用通达信软件买股票安全吗?
redsi总结_最佳实战
Controller Layer Coding Specification
After using the vxe-table row merge, there will be an extra column blank after the merged row.
对话360数字安全集团高级副总裁高瀚昭:政企提升数字安全“看见”能力需全盘考虑不可“以点带面”
内存管理(五)——内存回收
dll repair tool installation tutorial
ReentrantReadWriteLock Read-write lock
eyb: Creation process: build the environment to display the menu (1)
8. cuBLAS Development Guide Chinese version--cublasGetMatrix() and cublasSetMatrix() in cuBLAS
tensorRT(一)| tensorRT如何进行推理加速?
Realize the Circle Fill effect of Sprite and solve the problems in the atlas
B-end product design process--product positioning
redis总结_原理
mysql 主从复制部署
Navicat连接mysql一段时间未操作之后卡住几秒
内存管理(四)——虚拟内存
China University of Petroleum (Beijing) - "Workover Engineering" Phase III Online Work