Problem 1092

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
 1None.gif#include <stdio.h>
 2None.gif
 3None.gifint main()
 4ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 5InBlock.gif    int a = 1, b = 0;
 6InBlock.gif
 7InBlock.gif    while(a != 0)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        b = 0;
10InBlock.gif        while(getchar()!='\n')
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            scanf("%d"&a);
13InBlock.gif            b = b + a;
14ExpandedSubBlockEnd.gif        }

15InBlock.gif        printf("%d\n", b);
16InBlock.gif        scanf("%d"&a);
17ExpandedSubBlockEnd.gif    }

18ExpandedBlockEnd.gif}

 

Summary:
The iostream system is always a puzzling part for any programming language, so is C's.
1. DO NOT forget to initialize the value of b to ZERO in the beginning of each loop.
2. The scanf function ignores any whitespace character(blank, newline or tab) and make them the seperator for each next non-whitespace character that will be read. So either the "\n" or "space" can be treat as the terminal condition. I DID NOT use the number of addends as the condition(first number) as it's read by line 10.
3. Let's look into the whole process in detail.
Line 5, initialize each variable. As a will be used as the terminal condition, it's put to be 1.
Line 7, when the first value of each input loop gets 0, the whole process ends.
Line 9, initialize the value of b to ZERO in the beginning of each loop.
Then come to the next block. Suppose the input is 4 1 2 3 4. Line 10 reads each space character until the '\n' and line 12 retrieves the value of numbers.
Line 16, IMPORTANT, read the first value of the next new input loop to make it as the validation condition for line 7.

I had submitted the codes for several times before accepted.
184988_0_12269.c: In function `main':
184988_0_12269.c:14: error: syntax error before '/' token
Remember, the Online Judge Compiler seems not to support any referance with //.

转载于:https://www.cnblogs.com/NickyYe/archive/2007/02/05/640781.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值