ACM模式各种输入整理(C++)
创始人
2024-02-24 14:33:04
0

1.前言

本文整理ACM模式的各种输入形式。

2. ACM模式的输入种类

2.1 整形数组输入

2.1.1 在终端的一行中输入固定数目的整型数字,并存到数组中,中间以空格分隔

示例:

  • 3
  • 1 2 3

方法1

#include 
#include 
using namespace std;int main(){int n;cin >> n;vector nums(n);for (int i = 0; i < n; ++i){cin >> nums[i];}//测试:打印数组cout<<"test c++ input:"<

方法2 

#include 
#include 
using namespace std;int main(){int n;cin >> n;vector nums;nums.resize(n);for (int i = 0; i < n; ++i){cin >> nums[i];}//测试:打印数组cout<<"test c++ input:"<

方法3

#include 
#include 
using namespace std;int main(){int n;cin >> n;vector nums;for (int i = 0; i < n; ++i){int val;cin >> val;nums.push_back(val);}//测试:打印数组cout<<"test c++ input:"<

 正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
3
1 2 3
test c++ input:
1 2 3

2.1.2 在终端的一行中输入非固定数目的整型数字,并存到数组中,中间以空格(或者其他单字符,./)分隔

方法1

#include 
#include 
using namespace std;int main(){vector nums;int num;while(cin>>num){nums.push_back(num);if(getchar() == '\n')break;}//测试:打印数组cout<<"test c++ input:"<

方法2

#include 
#include 
using namespace std;int main(){//代码通过cin.get()从缓存中读取一个字节,这样就扩充了cin只能用空格和TAB两个作为分隔符。vector nums;int num;while(cin>>num){nums.push_back(num);if(cin.get() == '\n')break;}//测试:打印数组cout<<"test c++ input:"<

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
1 2 3 4
test c++ input:
1 2 3 4

2.1.3 在终端的一行中输入固定数目的整型数字,并存到数组中,中间以(其他单字符,./)分隔

示例:

  • 3
  • 1,2 ,3
#include 
#include 
using namespace std;int main(){int m;cin >>  m;char sep;vector nums(m);for (int i = 0; i < m - 1; ++i){cin >> nums[i] >> sep;}cin >> nums[m - 1];//测试:打印数组cout<<"test c++ input:"<

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
3
1,2,3
test c++ input:
1 2 3

2.2 字符串输入

2.2.1 给定一行字符串,每个字符串用空格间隔,一个样例为一行

示例:

  • daa ma yello

方法

#include 
#include 
using namespace std;int main(){string str;vector strs;while (cin >> str) {strs.push_back(str);if (getchar() == '\n') {  //控制测试样例for (auto& str : strs) {cout << "current character:"<< str << " ";}cout << endl;strs.clear();}}return 0;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
aaa bbb
current character:aaa current character:bbb

2.2.2 给定一行字符串,每个字符串用逗号间隔,一个样例为一行

方法:使用getline 读取一整行字符串到字符串input中,然后使用字符串流stringstream,读取单个数字或者字符。每个字符中间用','间隔

#include 
#include 
#include 
#includeusing namespace std;int main(){string input;while (getline(cin, input)) {  //读取一行vector strs;string str;stringstream ss(input);while(getline(ss, str,',')){strs.push_back(str);}sort(strs.begin(), strs.end());for (auto& str : strs) {cout << str << " ";}cout << endl;}return 0;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
d,v,b,c,f
b c d f v

2.2.3 给定一行字符串,每个字符串用空格间隔,一个样例为一行

#include 
#include 
#include using namespace std;int main(){string input;while (getline(cin, input)) {  //读取一行stringstream data(input);  //使用字符串流int num = 0, sum = 0;while (data >> num) {sum += num;}cout << sum << endl;}return 0;
}

正确性测试:

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
1 2 3 
6

2.3 字符串输入到数组

2.3.1 输入n行字符串,逗号分开,存到数组里

示例(假定每行输入5个数)

3

1,2,3,3,2

4,5,6,1,3

7,8,9,3,4

#includeusing namespace std;int main()
{int n;cin >> n;vector> vec(n,vector(5));for (int i = 0; i < n;++i){//注意这里一定要有i控制,用while容易一直输入导致错误string s;cin >> s;replace(s.begin(), s.end(), ',', ' ');istringstream input(s);string tmp;for (int j = 0; j < 5;++j){//内层循环也很重要input >> tmp;vec[i][j] = stoi(tmp);}}for (int i = 0; i < vec.size(); i++){for (int j = 0; j < vec[0].size(); j++){cout << "postion "<< i << j << " output is:"<< vec[i][j] << endl;}}return 0;
}

正确性测试

C:\Users\zhangyy\CLionProjects\Ctest\cmake-build-debug\Ctest.exe
2
1,2,3,4,5,
6,7,8,9,0
postion 00 output is:1
postion 01 output is:2
postion 02 output is:3
postion 03 output is:4
postion 04 output is:5
postion 10 output is:6
postion 11 output is:7
postion 12 output is:8
postion 13 output is:9
postion 14 output is:0Process finished with exit code 0

3. 最后

相对于leetcode模式,ACM模式需要自己写输入输出,但是常见输入输出也是几类。熟悉后,就不会在这方面浪费宝贵的时间了。

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...