ACM模式各种输入整理(C++)

1.前言

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

2. ACM模式的输入种类

2.1 整形数组输入

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

示例:

  • 3
  • 1 2 3

方法1

#include <iostream>
#include <vector>
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:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

方法2 

#include <iostream>
#include <vector>
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:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

方法3

#include <iostream>
#include <vector>
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:"<<endl;
    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 <iostream>
#include <vector>
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:"<<endl;
    for(auto i : nums)
        cout << i << " ";
    cout << endl;
}

方法2

#include <iostream>
#include <vector>
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:"<<endl;
    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 <iostream>
#include <vector>
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:"<<endl;
    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 <iostream>
#include <vector>
using namespace std;


int main(){
    string str;
    vector<string> 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 <iostream>
#include <vector>
#include <sstream>
#include<algorithm>

using namespace std;


int main(){
    string input;
    while (getline(cin, input)) {  //读取一行
        vector<string> 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 <iostream>
#include <vector>
#include <sstream>

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<bits/stdc++.h>

using namespace std;


int main()
{
    int n;
    cin >> n;
    vector<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模式需要自己写输入输出,但是常见输入输出也是几类。熟悉后,就不会在这方面浪费宝贵的时间了。

  • 5
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值