1094
A+B for Input-Output Practice (VI)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 85961 Accepted Submission(s): 57242
Problem Description
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
Author
lcy
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNextInt())
{
int n = s.nextInt(), sum = 0;
for(int i = 1; i <= n; i ++)
{
int a = s.nextInt();
sum += a;
}
System.out.println(sum);
}
}
}
本文详细解析了一道经典的A+B输入输出练习题,题目要求计算一系列整数的总和。通过Java代码示例,展示了如何读取多个测试用例,并计算每个用例中N个整数的总和。适合初学者练习输入输出及基本数学运算。
325

被折叠的 条评论
为什么被折叠?



