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
題意:每組數據線輸入一個整數,表示本組數據包含整數的個數,輸出這幾個整數的和,當數據第一個數是0時,程序結束。
分析:現在while語句的判斷條件里面判斷每組輸入數據的第一個數據是不是為0,然后在while語句里面用for循環語句輸入后面的各個整數。
AC源代碼(C語言):
1 #include<stdio.h> 2 int main() 3 { 4 int a,b,s,i; 5 while(scanf("%d",&a)&&a) 6 { 7 s=0; 8 for(i=1;i<=a;i++) 9 { 10 scanf("%d",&b); 11 s+=b; 12 } 13 printf("%d\n",s); 14 } 15 return 0; 16 }
2013-01-29