[TOC]
【C语言】蓝桥杯/ACM竞赛/A+B
上次参加了学校的蓝桥杯校队选拔“集训”
第一次“测试”就直接被考傻了,虽然都是我“好像”学过的内容,但我里里外外真的看不出来到底怎么写,太离谱了!
而且学长用的都是C++,我只学了c语言,后面的题目完全看不懂了。
当然,归根结底还是我太菜了
在这里把我搞了好久终于弄懂的A+B题目分享给大家
- 开始集训:A+B不有手就行?
- 结束后:我手呢?
注:题目是英文的!!!!
A+B for Input-Output Practice (I)
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 | 1 5 |
Sample Output
1 | 6 |
这道题看起来非常简单,我一上来就打上了两行scanf,啪的一下就提交了
然后系统啪的一下给我返回了一个大大的红色WRONG ANSWER
正确做法如下:
1 |
|
不能直接使用两行scanf的原因是
题目需要的是这串代码能完整地使用多次,而不是简单的只执行两次
在while里使用EOF的原因
这里使用eof,可以让程序在未输入错误的情况下一直进行循环计算a+b
scanf函数的返回值
scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF。
如:scanf(“%d %d”,&a,&b);
函数返回值为int型。如果a和b都被成功读入,那么scanf的返回值就是2;
如果只有a被成功读入,返回值为1;
如果a和b都未被成功读入,返回值为0;
如果遇到错误或遇到end of file,返回值为EOF。end of file为Ctrl+z 或者Ctrl+d。
[摘自百度知道用户@纵横竖屏的回答]
EOF的值为-1,但不能简单的用-1代替EOF
A+B for Input-Output Practice (II)
Problem Description
Your task is to Calculate a + b.
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.
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 | 2 |
Sample Output
1 | 6 |
这道题的意思其实就是在键入需要相加的数字之前,先键入需要相加的数字组数(也可以理解为行数)
要求我们用循环的方式完成“相加数字行数”的操作
完成n行后需要跳出该a+b的循环
1 |
|
A+B for Input-Output Practice (III)
Problem Description
Your task is to Calculate a + b.
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.
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 | 1 5 |
Sample Output
1 | 6 |
这道题的要求是a+b的程序在读取到两个0 0的时候会退出循环
且代码不会处理这两个0 0(把它们当作循环结束标志)
1 |
|
A+B for Input-Output Practice (IV)
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:
1 | 4 1 2 3 4 |
Sample Output:
1 | 10 |
与前面的两个数字相加不同,这道题的要求是,先输入你需要相加的数字的个数,再依次键入数字
遇到0的时候停止循环
- 这个0必须是第一个数字,即代表需要“0个数字相加”
- 如果是需要相加的数字里有0,不应退出循环
1 |
|
A+B for Input-Output Practice (V)
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 with one line of output for each line in input.
Sample Input:
1 | 2 |
Sample Output:
1 | 10 |
这道题和上一道题的要求差不多,结合了第二题的内容,即我们在键入需要相加的数字之前,要先键入“行数”
当然它也少了在遇到0的时候退出循环
1 |
|
A+B for Input-Output Practice (VI)
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:
1 | 4 1 2 3 4 |
Sample Output:
1 | 10 |
这道题和第四题很像,就缺少了遇到0停止
1 |
|
A+B for Input-Output Practice (VII)
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 | 1 5 |
Sample Output:
1 | 6 |
这道题多了一个要求,“followed by a blank line.”
需要我们在代码完成后,跟着打印一个空行,也就是\n
1 |
|
A+B for Input-Output Practice (VIII)
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:
1 | 3 |
Sample Output:
1 | 10 |
这道题的要求结合了上面的题目
- 需要先键入“行数”
- 需要在相加之前键入 需要相加的数字的个数
- 打印答案后需要再打印一个 空行
也不怕大家笑话,这道题是我抄的,原博客在这 [点我]
原博客中在for循环的scanf前多了一个if(i==0)的代码,我实在没看懂这句代码的意义,去掉之后系统依旧判对!
1 |
|
呼!终于写完这篇博客了
如果这对你有帮助,还请点赞关注收藏吧!
这对我真的很重要!
- 本文标题:【C语言】蓝桥杯/ACM竞赛入门 A+B for Input-Output Practice
- 创建时间:2021-10-26 20:14:28
- 本文链接:posts/2420712993/
- 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!