当前位置:网站首页>Hj8 consolidated statement record hj08
Hj8 consolidated statement record hj08
2022-07-16 08:40:00 【fareast_ mzh】
HJ8 Consolidated statement records
#include<iostream>
#include<map>
using namespace std;
int main(int argc, char *argv[]) {
int k, v;
int n = 0;
std::map<int, int> m;
cin >> n;
while (n-- > 0) {
cin >> k;
cin >> v;
if (m.find(k) == m.end()) {
m[k] = v;
} else {
m[k] += v;
}
}
for (auto it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << " " << it->second << "\n";
}
return 0;
}The actual data read is usually from the file , use std::ifstream, Need to introduce fstream The header file #include <fstream>

main.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
class HJ08 {
public:
static std::string file_get_contents(const std::basic_string<char>& path) {
std::ifstream file;
file.open(path, std::ios_base::in);
if (!file.is_open()) {
std::cout << " open \"" << path << "\" Failure ";
return "";
}
std::stringstream s;
s << file.rdbuf();
file.close();
return s.str();
}
static std::map<int, int> mergedPairs(const std::basic_string<char>& path) {
std::map<int, int> m;
std::ifstream ifs;
ifs.open(path, std::ios_base::in);
if (!ifs.is_open()) {
std::cout << " open \"" << path << "\" Failure ";
return m;
}
int n = 0;
ifs >> n;
int k, v;
while (n-- > 0) {
ifs >> k;
ifs >> v;
if (m.find(k) == m.end()) {
m[k] = v;
} else {
m[k] += v;
}
}
ifs.close();
return m;
}
};
int main() {
// std::string content = HJ08::file_get_contents("../input_01.txt");
// std::cout << content;
std::map<int, int> m;
m = HJ08::mergedPairs("../input_01.txt");
// std::map<int, int>::const_iterator
for (auto it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << " " << it->second << "\n";
}
// for (auto &pair : m) {
// std::cout << pair.first << " " << pair.second << "\n";
// }
return 0;
}
pair<int, int>i;
cin >> i.first >> i.second;
m[i.first] += i.second;
边栏推荐
- 2022 cloud native programming challenge starts! Tutor analysis service grid competition questions
- 一个XML文件例子
- Test basis 3
- PG运维篇--服务启停
- viewpager禁止左右滑动
- 测试基础4
- Pressing the volume key on the prototype can synchronously adjust the call volume of the mobile phone when Jerry is talking [chapter]
- Small program graduation project of wechat enterprise company (5) assignment
- Viewpager forbids sliding left and right
- Wechat classroom reservation of applet completion works applet graduation design (3) background function
猜你喜欢

快讯:京东科技发布“百亿收入计划”;博通软件业务总裁离职

CVPR | self enhanced unpaired image defogging based on density and depth decomposition

拔掉电源会怎样?GaussDB(for Redis)双活让你有备无患

Day102. Shangyitong project

微信支付APIV3统一回调接口封装(H5、JSAPI、H5、App、小程序)

Cat and dog classification - simple CNN

Huawei switch SEP double half ring design scheme and detailed configuration steps

测试基础1

Win11安全中心删除的文件如何恢复?

【无标题】
随机推荐
[I2C (Analog) drive ssd1306 OLED screen of Renesas ra6m4 development board]
CVPR | self enhanced unpaired image defogging based on density and depth decomposition
[GPIO of keys and LEDs of Renesas ra6m4 development board]
电脑共享文件打不开要如何解决
基于华为WAC双机VRRP热备份下旁挂三层组网隧道转发模式解决方案
杰理之IR 可能出现丢码问题【篇】
mySQL上的应用了解
Summary of some experiences in the process of R & D platform splitting
猫狗分类-简单CNN
(手工)【sqli-labs27、27a】报错回显、布尔盲注、过滤后注入
Chapter 54 implementation of discount and commodity category entity definition of business logic
STM32实现NRF24L01通信
Day102. Shangyitong project
手把手教你安装CUDA
杰理之播 LINEIN 提示音以后可能出现死机【篇】
Leetcode 735 planetary collision [stack simulation] the leetcode road of heroding
How to turn off the dareu keyboard light
HJ8 合并表记录 HJ08
Install MySQL database in CentOS system
Glue terraform ecology to kubernetes world