ACM模式输入


  • hasNext()方法会判断接下来是否有非空字符。如果有,则返回true,否则返回false
  • hasNextLine() 方法会根据行匹配模式去判断接下来是否有一行(包括空行),如果有,则返回true,否则返回false
  • hashNextInt()方法判断控制台接收是否为数字
  • 读一整行:String s = sc.nextLine();
  • 读一个整数:int n = sc.nextInt();
  • 读一个字符串:String s = sc.next();(以空格作为分隔符)
  • 读一个浮点数:double t = sc.nextDouble();
  • 判断是否有下一个输入可以用 sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()

类型A

输入描述:

1
2
3
输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。
1 5
10 20

输出描述:

1
2
3
输出a+b的结果  
6
30

代码

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.*;

public class Main{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()) {
int a = scan.nextInt();
int b = scan.nextInt();
System.out.println(a + b);
}
}
}

类型B

输入描述:

1
2
3
4
输入第一行包括一个数据组数t(1 <= t <= 100),接下来每行包括两个正整数a,b(1 <= a, b <= 1000)
2
1 5
10 20

输出描述:

1
2
3
输出a+b的结果  
6
30

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int k = Integer.parseInt(sc.nextLine());
for(int i = 0; i < k; i++){
String[] strs = sc.nextLine().split(" ");
int a = Integer.parseInt(strs[0]);
int b = Integer.parseInt(strs[1]);
System.out.println(a+b);
}
}
}

类型C

输入描述:

1
2
3
4
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
1 5
10 20
0 0

输出描述:

1
2
3
输出a+b的结果  
6
30

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int a = sc.nextInt();
int b = sc.nextInt();
if(a == 0 && b == 0){
return;
}
System.out.println(a+b);
}
}
}

类型D

输入描述:

1
2
3
4
5
6
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
4 1 2 3 4
5 1 2 3 4 5
0

输出描述:

1
2
3
每组数据输出求和的结果
10
15

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;

public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int n = in.nextInt();
if(n == 0){
break;
} else {
int sum = 0;
for(int i = 1;i <= n; i++){
int x = in.nextInt();
sum += x;
}
System.out.println(sum);
}
}
}
}

类型E

输入描述:

1
2
3
4
5
6
7
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
2
4 1 2 3 4
5 1 2 3 4 5

输出描述:

1
2
3
每组数据输出求和的结果
10
15

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int k = in.nextInt();
for(int i = 0; i < k; i++){
int l = in.nextInt();
int sum = 0;
for(int j = 0; j < l; j++){
int a = in.nextInt();
sum += a;
}
System.out.println(sum);
}
}
}

类型F

输入描述:

1
2
3
4
5
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
4 1 2 3 4
5 1 2 3 4 5

输出描述:

1
2
3
每组数据输出求和的结果
10
15

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int k = in.nextInt();
int sum = 0;
for(int i = 0; i < k; i++){
int a = in.nextInt();
sum += a;
}
System.out.println(sum);
}
}
}

类型G

输入描述:

1
2
3
4
5
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。
1 2 3
4 5
0 0 0 0 0

输出描述:

1
2
3
4
每组数据输出求和的结果
6
9
0

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int sum = 0;
String s = in.nextLine();
String[] str = s.split(" ");
for(int i = 0; i < str.length; i++){
sum += Integer.parseInt(str[i]);
}
System.out.println(sum);
}
}
}

类型H

输入描述:

1
2
3
4
输入有两行,第一行n
第二行是n个字符串,字符串之间用空格隔开
5
c d a bb e

输出描述:

1
2
输出一行排序后的字符串,空格隔开,无结尾空格
a bb c d e

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int a = Integer.parseInt(in.nextLine());
String s = in.nextLine();
String[] str = s.split(" ");
Arrays.sort(str);
for(int i = 0; i < a; i++){
System.out.print(str[i] + " ");
}

}
}

类型I

输入描述:

1
2
3
4
5
多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100
a c bb
f dddd
nowcoder

输出描述:

1
2
3
4
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开
a bb c
dddd f
nowcoder

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s = in.nextLine();
String[] str = s.split(" ");
Arrays.sort(str);
for(int i = 0; i < str.length; i++){
System.out.print(str[i] + " ");
}
System.out.println();
}
}
}

类型J

输入描述:

1
2
3
4
5
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
a,c,bb
f,dddd
nowcoder

输出描述:

1
2
3
4
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格
a,bb,c
dddd,f
nowcoder

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String s = in.nextLine();
String[] str = s.split(",");
Arrays.sort(str);
for(int i = 0; i < str.length; i++){
if(i == (str.length-1)){
System.out.print(str[i]);
}else{
System.out.print(str[i] + ",");
}
}
System.out.println();
}
}
}

类型K

输入描述:

1
2
输入有多组测试用例,每组空格隔开两个整数
1 1

输出描述:

1
2
对于每组数据输出一行两个整数的和 
2

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}

文章作者: Yang Shiyu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Yang Shiyu !
  目录