vj基本训练 HDU1092

这个是题目:
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
 

Author
lcy



这个题目的意思就是前面输入一个数字n;假如输了5
后面就输入这n个数字;最后得出这五个数字的和(前面输了5);


代码;
#include<stdio.h>
int main()
{
  int i,n,sum,a;
  while(scanf("%d",&n))
  {
    if(n!=0)
    {
      sum=0;
      for(i=0;i<n;i++)
      {
      scanf("%d",&a);
      sum+=a;
      }
      printf("%d\n",sum );
    }
    else
      break;
  }
}


解释:
先是输入一个数字,代表后面将会输入几个数字;
如果输入的是0,那就break,如果不是零那就进入
加法循环;
while  输入一个数字
判断这个数字是否是0;if是0,则进入加法循环;
else则break
在if里面,开始分情况
定义加出来的值是sum
则 先给sum个0(初始值)
之后进入for循环(加法)
for(i=0;i<n;i++)
代表加了n个数字;


printf不能写在for循环里面,这个挺考验会不会用for;
如果写到for里面就会出现下面这个情况;

4 1 2 3 4
1
3
6
10

全加了一遍,每次加法都被写出来了。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值