依据大佬建议的 刷题步骤(杭电oj题目)
题目01
linkOJ
@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
编码中遇到的问题
这题意思和1096差不多。只不过少了个输入规定行数。
题目中注意这句Input contains multiple test cases, and one case one line.
意思是输入只有遇到EOF
的时候才停止,不然就是输出一次后继续进行下一次输入。
测试结果
ojbk。
全代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
short sum;
while (scanner.hasNextShort()) {
sum = 0;
short counter = scanner.nextShort();
for (int i = 0; i < counter; i++) {
sum+=scanner.nextShort();
}
System.out.println(sum);
}
}
}
题目02
@description
*Your task is to calculate the sum of some integers.
- Input
- Input contains an integer N in the first line, and then N lines follow.
- Each line starts with a integer M, and then M integers follow in the same line.
- 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
- 2
- 4 1 2 3 4
- 5 1 2 3 4 5
- Sample Output
- 10
- 15
编码中遇到的问题
该题和1096唯一的不同就是输出换不换行。这题输出不换行。
测试结果
ojbk。
全代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
short n = scanner.nextShort();
short[] results = new short[n];
for (int i = 0; i < n; i++) {
short counter = scanner.nextShort();
for (int j = 0; j < counter; j++) {
results[i]+=scanner.nextShort();
}
}
for (int i = 0; i < n; i++) {
System.out.println(results[i]);
}
}
}
题目03
@description
- Your task is to Calculate a + b.
- Input
- Input contains multiple test cases. Each test case contains a pair of integers a and b,
- one pair of integers per line.
- A test case containing 0 0 terminates the input and this test case is not to be processed.
- Output
- For each pair of input integers a and b you should output the sum of a and b in one line,
- and with one line of output for each line in input.
- Sample Input
- 1 5
- 10 20
- 0 0
- Sample Output
- 6
- 30
编码中遇到的问题
这题好判断,以“0 0”输入为结束符。
测试结果
ojbk。
全代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
short[] results = new short[Short.MAX_VALUE];
short i = 0;
while (sc.hasNextShort()) {
short a = sc.nextShort();
short b = sc.nextShort();
if(a==0 && b==0) {
for (int j = 0; j < results.length; j++) {
if(results[j] == 0) {
break;
}
System.out.println(results[j]);
}
return;
}
results[i] = (short) (a+b);
i++;
}
}
}