1. 程式人生 > >A+B for Input-Output Practice (VI)--JAVA

A+B for Input-Output Practice (VI)--JAVA

題目:

Your task is to calculate the sum of some integers. 

Input

Input contains multiple test cases, and one case one line. Each case starts with an integer N, and then N integers follow in the same line. 

Output

For each test case you should output the sum of N integers 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

Sample Output

10
15

題意:

給你一個數字n,代表後面跟著n個數字,然後讓你求出來n個數字的和;

程式碼如下:

C++:

#include<stdio.h>

int main()
{
    int n,m,s;
    while(~scanf("%d",&n))
    {
        s=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&m);
            s+=m;
        }
        printf("%d\n",s);
    }
    return 0;
}

JAVA:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
	Scanner input=new Scanner(System.in);
	int m,a,s;
	while(input.hasNext()) {
		m=input.nextInt();
		s=0;
		for(int i=0;i<m;i++) {
			a=input.nextInt();
			s=s+a;
		}
		System.out.println(s);
	}
}
}