本文整理ACM模式的各种输入形式。
示例:
方法1
- #include
- #include
- using namespace std;
-
-
- int main(){
- int n;
- cin >> n;
- vector<int> nums(n);
- for (int i = 0; i < n; ++i){
- cin >> nums[i];
- }
- //测试:打印数组
- cout<<"test c++ input:"<
- for(auto i : nums)
- cout << i << " ";
- cout << endl;
- }
方法2
- #include
- #include
- using namespace std;
-
-
- int main(){
- int n;
- cin >> n;
- vector<int> nums;
- nums.resize(n);
- for (int i = 0; i < n; ++i){
- cin >> nums[i];
- }
- //测试:打印数组
- cout<<"test c++ input:"<
- for(auto i : nums)
- cout << i << " ";
- cout << endl;
- }
方法3
- #include
- #include
- using namespace std;
-
-
- int main(){
- int n;
- cin >> n;
- vector<int> nums;
- for (int i = 0; i < n; ++i){
- int val;
- cin >> val;
- nums.push_back(val);
- }
- //测试:打印数组
- cout<<"test c++ input:"<
- for(auto i : nums)
- cout << i << " ";
- cout << endl;
- }
正确性测试:
- 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<int> nums;
- int num;
- while(cin>>num){
- nums.push_back(num);
- if(getchar() == '\n')
- break;
- }
- //测试:打印数组
- cout<<"test c++ input:"<
- for(auto i : nums)
- cout << i << " ";
- cout << endl;
- }
方法2
- #include
- #include
- using namespace std;
-
-
- int main(){
- //代码通过cin.get()从缓存中读取一个字节,这样就扩充了cin只能用空格和TAB两个作为分隔符。
- vector<int> nums;
- int num;
- while(cin>>num){
- nums.push_back(num);
- if(cin.get() == '\n')
- break;
- }
- //测试:打印数组
- cout<<"test c++ input:"<
- for(auto i : nums)
- cout << i << " ";
- cout << endl;
- }
正确性测试:
- 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<int> nums(m);
-
- for (int i = 0; i < m - 1; ++i){
- cin >> nums[i] >> sep;
- }
- cin >> nums[m - 1];
- //测试:打印数组
- cout<<"test c++ input:"<
- for(auto i : nums)
- cout << i << " ";
- cout << endl;
- }
正确性测试:
- 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
- #include
-
- using 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
- #include
-
- using namespace std;
-
-
- int main()
- {
- int n;
- cin >> n;
- vector
int>> vec(n,vector<int>(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:0
-
- Process finished with exit code 0
3. 最后
相对于leetcode模式,ACM模式需要自己写输入输出,但是常见输入输出也是几类。熟悉后,就不会在这方面浪费宝贵的时间了。
-
相关阅读:
16位 (MCU) R7F101G6G3CSP、R7F101G6E3CSP、R7F101G6G2DSP、R7F101G6E2DSP是新一代RL78通用微控制器
CJSON解析json字符串示例
DDD - 事件风暴从理论到落地
微服务框架 SpringCloud微服务架构 5 Nacos 5.3 服务多级存储模型
32GB Jetson Orin SOM 不能刷机问题排查
#你我都是国家队#,与泸州老窖一起为中国荣耀干杯
ThreadLocal
LeetCode 面试题 08.12. 八皇后
部署主从数据库
MybatisPlus的Id填充笔记整理
-
原文地址:https://blog.csdn.net/qq_33163046/article/details/128066918
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU