C语言 | 杭州电子科技大学ACM | 求和问题 | 参考代码 “just for 参考”

所用语言:C语言

题目来源:杭州电子科技大学ACM(网址:ACM

题目范围:Section 1

注;文中所给的参考代码仅仅是一种思路,不一定是最优解。
·

·····························分······························割······························线·····························

·

1.1.1

原题描述

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

参考代码

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS				//vs2019消除scanf函数警告的宏定义
#include <stdio.h>

int main(void)
{
    
    
	int a, b;

	while (scanf("%d %d", &a, &b) != EOF)
	{
    
    
		printf("%d\n", a + b);
	}
	
	return 0;
}

关于参考代码里的“EOF”可以在VS中转到文档查找
在这里插入图片描述
在stdio.h头文件中的第37行,有一个宏定义
把 EOF 的值设为了 -1
在这里插入图片描述

EOF 是 End Of File的意思,用作文件结束标志。
该宏定义在stdio.h中,从数值角度看,就是-1
在Linux系统之中,EOF根本不是一个字符,而是当系统读取到文件结尾,所返回的一个信号值(也就是-1)。
一般C在读取数据时,都是按流模式进行数据读操作,这里的流可以是文件,也可以是标准输入(stdin,即键盘)。即:EOF可以表示文件结尾,还可以表示标准输入的结尾。
但是,标准输入与文件不一样,无法事先知道输入的长度,必须手动输入一个字符,表示到达EOF。
1、Linux中,在新的一行的开头,按下Ctrl+D,就代表EOF
2、Windows中,在新的一行的开头,按下Ctrl+Z,就表示EOF。

扫清了这个障碍,就能更好地理解while循环了。

这题的核心就是while循环。
题目中并没有说终止条件,但我们可以用 scanf() 函数的返回值来作为循环的判断条件。

关于scanf()函数的返回值,可以用一段代码测试一下

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)

{
    
    
	int x, y, z;
	
	printf("测试scanf函数的返回值\ninput:");

	printf("只输入 一 个数时,scanf的返回值为:%d\n", scanf("%d", &x));
	printf("当输入 两 个数时,scanf的返回值为:%d\n", scanf("%d %d", &x,&y));
	printf("当输入 三 个数时,scanf的返回值为:%d\n", scanf("%d %d %d", &x,&y,&z));

	printf("当输入的数据异常,scanf的返回值为:%d\n", scanf("%d %d %d", &x, &y, &z));

	printf("当输入遇到文件末,scanf的返回值为:%d\n", scanf("%d %d %d", &x, &y, &z));

}

运行结果
在这里插入图片描述
这里输入异常貌似程序就直接结束了
在这里插入图片描述
这里第一次输入正常
第二次连续输入了三次(都包含了~z,键盘操作:CTRL + Z),返回值为-1(我用的是VS2019,好像要输入三次~Z才是EOF)。
第三次输入了 ~D (键盘操作:CTRL + D),返回值为0,程序结束。

注意:这里的结束可能是输入流异常的结束,不是说程序异常的结束,可以看出来程序里的printf函数都是执行了的。

这个题目就是只有书本里的知识的话可能连EOF都不知道,老师也没讲scanf()函数的返回值,也许这就是理论与实操的不同吧,这才是刚开始呢hhh
题目也说了是给ACM初学者锻炼的啦。
·

·····························分······························割······························线·····························

·

1.1.2

原题描述

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
2
1 5
10 20
Sample Output
6
30

这题有点不一样了,个人感觉比第一题好做,也更好理解(因为我当初不知道EOF这玩意儿hhh),这题明确的告诉我们要算几次了,先输入个N,用个简单的循环就解决了。

参考代码

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
    
    
	int N;		
	int a, b;

	scanf("%d", &N);

	while (N--)
	{
    
    
		scanf("%d %d", &a, &b);
		printf("%d\n", a + b);
	}
	
	return 0;
}

·

·····························分······························割······························线·····························

·

1.1.3

原题描述

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 5
10 20
0 0
Sample Output
6
30

这题的循环终止条件也是很明确的——当输入的两个整数都是0的时候。

参考代码

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
    
    
	int a, b;

	while (1)		//死循环
	{
    
    
		scanf("%d %d", &a, &b);
		if (a == 0 && b == 0)
		{
    
    
			break;	//直接用 return 0; 似乎没有太大问题
		}
		printf("%d\n", a + b);
	}
	
	return 0;
}

关于这题,我用的是死循环,然后输入数据,进行判断,最后输出结果。
代码简单,只有一个while循环,再无其它东西,break跳出一层循环之后只有一条返回语句,所以判断为真的话直接return 0 似乎也没有太大问题。

·

·····························分······························割······························线·····························

·

1.1.4

原题描述

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

参考代码

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
    
    
	int N;
	int num;
	int sum;

	while (1)
	{
    
    
		scanf("%d", &N);
		if (N==0)
		{
    
    
			break;	// return 0; 也可
		}
		sum = 0;
		while (N--)	//累加求和,sum记得初始化为0哦
		{
    
    
			scanf("%d", &num);
			sum = sum + num;
		}
		printf("%d\n", sum);

	}
	
	return 0;
}

·

·····························分······························割······························线·····························

·

1.1.5

原题描述

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 with one line of output for each line in input.
Sample Input
2
4 1 2 3 4
5 1 2 3 4 5
Sample Output
10
15

参考代码

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
    
    
	int N;
	int M;
	int num;
	int sum;

	scanf("%d", &N);
	
	while (N--)
	{
    
    
		sum = 0;

		scanf("%d", &M);
		
		while (M--)
		{
    
    
			scanf("%d", &num);
			sum = sum + num;
		}
		
		printf("%d\n", sum);

	}
	
	return 0;
}

·

·····························分······························割······························线·····························

·

1.1.6

原题描述

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

参考代码

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
    
    
	int N;
	int num;
	int sum;

	
	while (scanf("%d", &N)!=EOF)
	{
    
    
		sum = 0;

		while (N--)
		{
    
    
			scanf("%d", &num);
			sum = sum + num;
		}
		
		printf("%d\n", sum);
	}
	
	return 0;
}

这个似乎也是终止条件不明的亚子。小问题,参照1.1.1。

·

·····························分······························割······························线·····························

·

1.1.7

原题描述

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

参考代码

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

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

似乎并没有太难,也许只是个铺垫呢。

·

·····························分······························割······························线·····························

·

1.1.8

原题描述

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
错误示范

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
    
    
	int N;
	int M;
	int num;
	int sum;

	scanf("%d", &N);

	while (N--)
	{
    
    
		scanf("%d", &M);
		sum = 0;
		while (M--)
		{
    
    
			scanf("%d", &num);
			sum += num;
		}
		printf("%d\n\n", sum);
	}

	return 0;
}

我承认我一开始就是这么写的hhh,然后系统一直说我 呈现错误,我是真的烦。

其实大部分都是正确的,只不过是换行的问题,这题和1.1.7的题目描述8太一样。

1.1.7要求是一行结果带一个换行
而1.1.8要求的是在输出结果之间带一个空行(意味着最后一行的后面不能带空行,这就是错误示范的错误)

正确的参考代码应该是这样的:

/*
 * 	编译环境:VS2019
 *	编译时间:2020-7-17
 *	作    者:童话与云
 */

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
    
    
	int N;
	int M;
	int num;
	int sum;

	scanf("%d", &N);

	while (N--)
	{
    
    
		scanf("%d", &M);
		sum = 0;
		while (M--)
		{
    
    
			scanf("%d", &num);
			sum += num;
		}
		printf("%d\n", sum);	//注意这里只有一个  '\n' 哦
		if (N)  				//等价于 if(N != 0)  就是在判断是否为最后一个结果啦
		{
    
    
			putchar(10);
		}
	}

	return 0;
}

大概就是这样啦。

猜你喜欢

转载自blog.csdn.net/l2754283833/article/details/107554019
今日推荐