Idiot-maker

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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
 1#include <stdio.h>
 2
 3int main()
 4{
 5    int a = 1, b = 0;
 6
 7    while(a != 0)
 8    {
 9        b = 0;
10        while(getchar()!='\n')
11        {
12            scanf("%d"&a);
13            b = b + a;
14        }

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

18}

 

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 //.

posted on 2007-02-05 15:51  NickyYe  阅读(339)  评论(1编辑  收藏  举报