【ACM】入门训练之A+B

前言

问:说说你印象最深刻的程序
A答:Hello World
B答:A+B

A+B for Input-Output Practice (I)

注:后续的A+B将只有原题链接,没有题目详情哈。建议和我一样的acm小白点击下题目链接,AC

原题链接

Problem Description

Your task is to Calculate a + b.
Too easy?! Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

Sample Input

1 5
10 20

Sample Output

6
30

sanwa:我感觉吧,这道题目我还是会做的,像我这么菜的都会的题,肯定没什么说的啦。可能需要注意的点就是输入的样例数是没有限制的,注意输入结束条件即可。我贴下代码,大佬看看有没有什么错或者改进的地方^_^

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

A+B for Input-Output Practice (II)

原题链接

INPUT

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.

sanwa:这道题目和前边不一样的地方在于,确定了输入的数据数量,另外注意的是两道题目的A和B都是integer。大佬看看有没有什么错或者改进的地方^_^

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

A+B for Input-Output Practice (III)

原题链接

Input

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.

sanwa:这个主要是读懂当A和B都同时为零时结束输入,如何构造这种情况呢?

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

小结: 总的来说,前边三道题目都是给定了A和B,求A+B的值,主要的不同点在于对于结束输入条件(数据量的限定),旨在让我们了解不同的输入结束条件下如何进行正确的接收数据和处理数据。

A+B for Input-Output Practice (IV)

原题链接

Problem Description

Your task is to Calculate the sum of some integers.

Input

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.

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

Sample Input

4 1 2 3 4
5 1 2 3 4 5
0

Sample Output

10
15

sanwa:很显然,这道题目感觉就是前边三道题目的一个综合,有样例数据的结束条件(输入0),也有单个样例的输入结束条件(给定了n)。sanwa的代码是这样写的,但感觉不是很优雅,大佬看看给下更优雅美观的代码 ^_^

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

A+B for Input-Output Practice (V)

原题链接

Input

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.

Output

For each group of input integers you should output their sum in one line, and with one line of output for each line in input.

sanwa:这道题目主要也是前三道题目的综合,用integer N限定了样例数,用integer M限定了单个样例的数据量. dalao dalao,给我看看代码,^_^

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

A+B for Input-Output Practice (VI)

原题链接

Problem Description

Your task is to calculate the sum of some integers.

Input

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.

Output

For each test case you should output the sum of N integers in one line, and with one line of output for each line in input.

Sample Input

4 1 2 3 4
5 1 2 3 4 5

Sample Output

10
15

sanwa : 这道题目看着感觉怪怪的,就直接给出了这个,感觉有点突兀。但细想下,还是practice I、II、III的一个综合。也就是没有限制样例数目(以文件结尾为结束),单个样例用integer n来限定

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

小结: 这三道题目都是前边三道题目的一定的综合,同样的也是对于不同输入的接收的训练吧,不知道说得对不对,说错了dalao纠正下 ^_^

A+B for Input-Output Practice (VII)

原题链接

Problem Description

Your task is to Calculate a + b.

Input

The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the sum of a and b, and followed by a blank line.

Sample Input

1 5
10 20

Sample Output

6

30

注:每个输出有空行
#include <iostream>
using namespace std;
int main(){
    long a,b;
    while(cin>>a>>b){
        cout<<a+b<<endl<<endl;
    }
    return 0;
}

A+B for Input-Output Practice (VIII)

原题链接

Problem Description

Your task is to calculate the sum of some integers.

Input

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.

Output

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.

Sample Input

3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output

10

15

6
注:每两个输出之间有有一空行

sanwa:这个题目可谓是前边各个问题的大综合了,用N去限定样例数目,用M去限定单个样例的数据量,然后输出结果之间都要求要有一个空行,对于这个输出空行,因为不是每一个样例结果都输出空行,我使用了一个标识,dalao们一般都是怎么做的呢?^_^。

#include <iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    bool flag = false;
    while(n--){
        int m;
        cin>>m;
        long long res = 0;
        while(m--){
            int temp;
            cin>>temp;
            res += temp;
        }
        if(flag){
            cout<<endl;
        } else{
            flag = true;
        }
        cout<<res<<endl;
    }
    return 0;
}

总结

通过这几道题目的练习,对于我们这样的ACM beginner来说,真是收获蛮大的,这几道题目主要是让我们了解到了不同的场景的输入方式以及对于这些输入数据的一个接收和处理的过程,同时,在输出结果也有一定的限制,特别是对于输出结果之间的空行的一个处理,很有意思。^_^
dalao们,文中有错误的地方,在此处跪求指正,另外对于我们这些小白有什么建议的还望不吝赐教,^_^

阅读更多
版权声明:本文为博主原创文章,转载请注明出处,谢谢。 https://blog.csdn.net/chensanwa/article/details/80347946
文章标签: ACM
个人分类: ACM 蓝桥杯
所属专栏: ACMer
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭