Post

ACM输入输出

ACM输入输出

为了准备即将到来的秋招,本篇文章主要讲解acm输入输出模式,这是因为秋招笔试中大部分笔试都要求我们进行acm模式的输入输出,而不是核心代码模式。

由于本人习惯使用cpp来刷题,所以本篇的acm输入输出模式的介绍也是cpp模式。

整型数组输入

示例:

  • 3
  • 1 2 3
1
2
3
4
5
6
7
8
int main() {
    int n;
    cin >> n;
    vector<int> nums(n);
    for(int i = 0; i < n; i++) {
        cin >> nums[i];
    }
}

在终端的一行中输入非固定数量的数字,文本结束符为结束。

示例:

  • 1 2 3 EOF
1
2
3
4
vector<int> nums;
while(cin>>n) {
    nums.push_back(n);
}

在终端中的一行中输入固定数目的整型数字,并保存到数组中,中间以某些符号分割。

示例:

  • 3
  • 1,2,3
1
2
3
4
5
6
7
8
9
10
int main() {
    int n;
    char sep;
    cin>>n;
    vector<int> nums(n);

    for(int i = 0; i < n; i++) {
        cin>>nums[i]>>sep;
    }
}

字符串输入

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

示例:

  • daa ma yello
1
2
3
4
5
6
7
int main() {
    string str;
    vector<string> strs;
    while(cin>>str) {
        strs.push_back(str);
    }
}

给定一行字符串,每个字符串逗号分割,一个样例为一行

示范:

  • aaa,bbb,ccc
  • ddd,ee,edd
1
2
3
4
5
6
7
8
9
10
11
int main() {
    string str;
    while(getline(cin, str)) {
        vector<string> strs;
        string input;
        stringstream ss(input);
        while(getline(ss, str, ',')) {
            strs.push_back(str);
        }
    }
}
This post is licensed under CC BY 4.0 by the author.