2020-12-16HDOJ-ACMsteps笔记

这篇博客介绍了如何使用C语言编写程序来读取一组整数并计算它们的总和。程序通过while循环读取每行的整数数量和实际数值,然后用while(a--)计数器替代传统计数器,简化代码。对于每一组输入,程序输出对应的整数和,并在每次计算后将和重置为零,以避免累加错误。
摘要由CSDN通过智能技术生成

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

#include <stdio.h>
int  main()
{
	int n,i,a,new,sum=0;
	scanf("%d",&n);
	i=0;
		while (~scanf("%d",&a)&&i<n)
		{
			while(a--)
			{
				scanf("%d",&new);
				sum+=new;
			}
			printf("%d\n",sum);
			i++;
			sum=0;/*一定要把sum重新置零,不然会
					带着上一行输入的sum继续做加法*/
		}
	return 0;
}

也改了许多次才通过.需要特别注意注释处的细节
还有,学到方法while(a–)来代替计数器.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值