ACM基本输入输出(C++语言)

杭电ACM Steps

最近在短学期的任务下,我开始啦杭电的ACM Steps模块。

其实在4月份左右做过一次ACM的基本输入输出,当时使用的全都是C语言,到现在基本都遗忘啦!

目录

1,A+B for Input-Output Practice (I)
2,A+B for Input-Output Practice (II)
3,A+B for Input-Output Practice (III)
4,A+B for Input-Output Practice (IV)
5,A+B for Input-Output Practice (V)
6,A+B for Input-Output Practice (VI)
7,A+B for Input-Output Practice (VII)
8,A+B for Input-Output Practice (VIII)

如今使用C++确实比C语言更简单,那么今天进行总结一下吧!

(图片是昨晚截的,奇怪的是这一串时间只有我一个在提交代码,全部显示的是本人提交代码,真的是太难得!我就想截个图纪念一下,哈哈哈哈!!!)
在这里插入图片描述
文章中代码都已Accept

1. The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.(连续输入一对整数a和b)
A+B for Input-Output Practice (I)

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    while(cin >> a >> b)
    {
        cout << a + b << endl;
    }
    return 0;
}

2. Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.(先输入N然后输入N行的一对整数a和b)
A+B for Input-Output Practice (II)

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    int n;
    cin >> n;
    while(n--){

        while(cin >> a >> b)
        {
            cout << a + b << endl;
        }
    }
    return 0;
}

3. Input contains multiple test cases. Each test case contains a pair of
integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
(包含多个测试用例,每个测试用例包含一对整数a和b,只有一对整数都为0,那么终止测试)
A+B for Input-Output Practice (III)

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    while(cin >> a >> b && (a || b))
    {
        cout << a + b << endl;
    }
    return 0;
}

4. Input contains multiple test cases. Each test case contains a integer N, and then N integers follow in the same line. A test case starting with 0 terminates the input and this test case is not to be processed.
(每个测试用例中先输入N,然后输入N个整数,一旦N为0那么终止输入)
A+B for Input-Output Practice (IV)

#include <iostream>
using namespace std;
int main()
{
    int temp;
    int sum ;
    int n;
    while(cin >> n && n)
    {
        sum = 0;//这里每次必须置为0
        while(n--){
            cin >> temp;
            sum += temp;
        }
        cout << sum << endl;
    }
    return 0;
}

5. Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
(先输入N代表需要输入N个测试用例,在每个测试用例前先输入M,接着输入M个整数)
A+B for Input-Output Practice (V)

#include <iostream>
using namespace std;
int main()
{
    int temp;
    int sum ;
    int n, m;
    cin >> n;
    while(n--){
       cin >> m;
       sum = 0;
       while(m--)
       {
           cin >> temp;
           sum += temp;
       }
        cout << sum << endl;
    }
    return 0;
}

6. Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line.
(输入多个测试用例,每组测试用例前输入N,接着输入N个整数)
A+B for Input-Output Practice (VI)

#include <iostream>
using namespace std;
int main()
{
    int temp;
    int sum ;
    int m;
   while(cin >> m)
   {
       sum = 0;
        while(m--)
       {
           cin >> temp;
           sum += temp;
       }
        cout << sum << endl;
    }
    return 0;
}

前六个基本输入输出基本包含所有的,无需死记硬背。看题目输入的要求即可,重点是下面两个的输出去要求(格式的问题)

7. The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.(输入连续的测试用例,每个测试用例包含一对整数,输入要求和1是一样的)
For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line(要求每输出一个结果必须中间空一行)
A+B for Input-Output Practice (VII)

#include <iostream>
using namespace std;
int main()
{
    int a, b;
    while(cin >> a >> b)
    {
    //直接在原来基础上cout << a + b << endl;增加一个endl
        cout << a + b << endl << endl;
    }
    return 0;
}

8. Input contains an integer N in the first line, and then N lines follow. Each line starts with a integer M, and then M integers follow in the same line.
(先输入N,然后输入N行测试用例,每个用例先输入M,接着输入M个整数,输入要求和5是一眼的)
For each group of input integers you should output their sum in one line, and you must note that there is a blank line between outputs.
(输出结果占一行,而且连个结果之间需要空一行)

#include <iostream>
using namespace std;
int main()
{
    int temp;
    int sum ;
    int n, m;
    cin >> n;
    while(n--){
       cin >> m;
       sum = 0;
       while(m--)
       {
           cin >> temp;
           sum += temp;
       }
       if (n != 0)
        cout << sum << endl << endl;
        else
        cout << sum << endl;
        //注意点:
        /*
        if (n != 0)
        cout << sum << endl << endl;
        else
        cout << sum ;//会报Presentation Error
        */
        
        /*     
          cout << sum << endl << endl;
          //此情况也会报Presentation Error
        */
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章