[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
2
1 5
10 20

Sample Output 

1
2
6
30 

这道题看起来非常简单,我一上来就打上了两行scanf,啪的一下就提交了

然后系统啪的一下给我返回了一个大大的红色WRONG ANSWER

正确做法如下:

1
2
3
4
5
6
7
#include<stdio.h>  
int main()  
{ 
   int a,b; 
   while(scanf("%d%d",&a,&b)!=EOF)
printf("%d\n",a+b);  
}   

不能直接使用两行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
3
2
1 5
10 20

Sample Output 

1
2
6
30

这道题的意思其实就是在键入需要相加的数字之前,先键入需要相加的数字组数(也可以理解为行数)

要求我们用循环的方式完成“相加数字行数”的操作

完成n行后需要跳出该a+b的循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
int main()
{
int a, b;
int i, j;
int n;
scanf("%d", &n);//n即为行数
int sum[20] = { 0 };
for (i = 0; i < n; i++)
{
scanf("%d %d", &a, &b);
sum[i] = a + b;
}
for (j = 0; j < n; j++)
{
printf("%d\n", sum[j]);
}
}

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
2
3
1 5 
10 20
0 0 

Sample Output 

1
2
6
30

这道题的要求是a+b的程序在读取到两个0 0的时候会退出循环

且代码不会处理这两个0 0(把它们当作循环结束标志)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<stdio.h>
int main()
{
int a = 0, b = 0, c;
while (1)
{
scanf("%d%d", &a, &b);
if (a == 0 && b == 0)//判断键入的是否为0 0
{
break;//是0 0,退出循环
}
else//不是0 0,继续运行a+b
{
c = a + b;
printf("%d\n", c);
}

}
return 0;
}

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
2
3
4 1 2 3 4
5 1 2 3 4 5
0

Sample Output:

1
2
10
15

与前面的两个数字相加不同,这道题的要求是,先输入你需要相加的数字的个数,再依次键入数字

遇到0的时候停止循环

  • 这个0必须是第一个数字,即代表需要“0个数字相加”
  • 如果是需要相加的数字里有0,不应退出循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main()
{
int n, sum, i, t;
while (scanf("%d", &n) != EOF && n != 0) //判断是否等于0
{
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &t);
sum = sum + t;//不断往sum里面加数字
}
printf("%d\n", sum);
}
}

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
3
2
4 1 2 3 4
5 1 2 3 4 5

Sample Output:

1
2
10
15

这道题和上一道题的要求差不多,结合了第二题的内容,即我们在键入需要相加的数字之前,要先键入“行数”

当然它也少了在遇到0的时候退出循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>

int main()
{
int n, a, b, i, j, sum;
sum = 0;
while (scanf("%d\n", &n) != EOF)
{
for (i = 0; i < n; i++)
{
scanf("%d", &b);
for (j = 0; j < b; j++)
{
scanf("%d", &a);
sum += a;
}
printf("%d\n", sum);
sum = 0;
}
}
}

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
2
4 1 2 3 4
5 1 2 3 4 5

Sample Output:

1
2
10
15

这道题和第四题很像,就缺少了遇到0停止

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
int main()
{
int n, sum, i, t;
while (scanf("%d", &n) != EOF)
{
sum = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &t);
sum = sum + t;
}
printf("%d\n", sum);
}
}

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
2
1 5
10 20

Sample Output:

1
2
6
30

这道题多了一个要求,“followed by a blank line.”

需要我们在代码完成后,跟着打印一个空行,也就是\n

1
2
3
4
5
6
7
8
9
10
#include<stdio.h>

int main()
{
int a, b;
while (scanf("%d %d", &a, &b) != EOF)
printf("%d\n", a + b);
printf("\n");//打印空行
return 0;
}

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
2
3
4
3
4 1 2 3 4
5 1 2 3 4 5
3 1 2 3

Sample Output:

1
2
3
4
5
10

15

6

这道题的要求结合了上面的题目

  • 需要先键入“行数”
  • 需要在相加之前键入 需要相加的数字的个数
  • 打印答案后需要再打印一个 空行

也不怕大家笑话,这道题是我抄的,原博客在这 [点我]

原博客中在for循环的scanf前多了一个if(i==0)的代码,我实在没看懂这句代码的意义,去掉之后系统依旧判对!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
int main()
{
int test,t=0;
scanf("%d",&test);//行数
while(test--)//每执行一次,减少一行
{
int s=0,i;
int n,a[1000];//用数组的方式完成相加
scanf("%d",&n);//需要相加的数字的个数
for(i=0;i<n;i++)
{

scanf("%d",&a[i]);
s+=a[i];
}
t++;
if(t>1)
printf("\n");
printf("%d\n",s);
}
return 0;
}

呼!终于写完这篇博客了

如果这对你有帮助,还请点赞关注收藏吧!

这对我真的很重要!