ACM模式
什么是ACM输入模式呢? 就是自己构造输入数据格式,把要需要处理的容器填充好,OJ不会给你任何代码,包括include哪些函数都要自己写,最后也要自己控制返回数据的格式。
注意类名必须为Main, 不要有任何package xxx信息
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { //注意while处理多个case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
注意hasNext和hasNextLine的区别:
采用java语言的话,有的同学这样处理输入输出 :
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
int n = in.nextInt();
//处理代码略
}
牛客网的系统会提示错误,但是如果把hasNextLine改成hasNext或者hasNextInt的话,就会提示正确 :
while (in.hasNextInt()) {
int n = in.nextInt();
//处理代码略
}
hasNextLine和hasNext的区别。
-
采用has xxxx的话,后面也要用next xxxx。比如前面用hasNextLine,那么后面要用 nextLine 来处理输入。
-
hasNext是检查是否有非空字符。判断接下来是否有非空字符.如果有,则返回true,否则返回false。
-
hasNextLine是检查输入中是否还有linePattern。其中LinePattern其实是匹配一个正则表达式。 根据行匹配模式去判断接下来是否有一行(包括空行),如果有,则返回true,否则返回false。
题目
A. 多组空格分隔的两个正整数
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
输出a+b的结果
1 5
10 20
6
30
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
B.第一行组数接空格分隔的两个正整数
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)
输出a+b的结果
2
1 5
10 20
6
30
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a + b);
}
}
}
C.空格分隔的两个正整数为0 0 结束
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
输出a+b的结果
1 5
10 20
0 0
6
30
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
if(a ==0 && b == 0) break;
System.out.println(a + b);
}
}
}
D.每行第一个为个数后带空格分割整数为0结束
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
每组数据输出求和的结果
4 1 2 3 4
5 1 2 3 4 5
0
10
15
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
if(n == 0) break;
int sum = 0;
for (int i = 0; i < n; i++) {
sum += in.nextInt();
}
System.out.println(sum);
}
}
}
E.第一行组数接第一个个数接空格分开的整数
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
每组数据输出求和的结果
2
4 1 2 3 4
5 1 2 3 4 5
10
15
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
int n = in.nextInt();
if(n == 0) break;
int sum = 0;
while (n > 0) {
sum += in.nextInt();
n--;
}
System.out.println(sum);
}
}
}
F.每行第一个为个数后带空格分割整数
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
每组数据输出求和的结果
4 1 2 3 4
5 1 2 3 4 5
10
15
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
int sum = 0;
while (n > 0) {
sum += in.nextInt();
n--;
}
System.out.println(sum);
}
}
}
G.多组空格分隔的正整数
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。
每组数据输出求和的结果
1 2 3
4 5
0 0 0 0 0
6
9
0
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] s = in.nextLine().split(" ");
int sum = 0;
for (int i = 0; i < s.length; i++) {
sum += Integer.parseInt(s[i]);
}
System.out.println(sum);
}
}
}
H.第一行个数第二行字符串
输入有两行,第一行n
第二行是n个空格隔开的字符串
输出一行排序后的字符串,空格隔开,无结尾空格
5
c d a bb e
a bb c d e
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
while (in.hasNext()) { // 注意 while 处理多个 case
String[] s = in.nextLine().split(" ");
Arrays.sort(s);
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
}
}
}
}
I.多行空格分开的字符串
多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符,n<100
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开
a c bb
f dddd
nowcoder
a bb c
dddd f
nowcoder
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] s = in.nextLine().split(" ");
Arrays.sort(s);
for (String c:s) {
System.out.print(c + " ");
}
System.out.println();
}
}
}
J.多行逗号分开的字符串
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格
a,c,bb
f,dddd
nowcoder
a,bb,c
dddd,f
nowcoder
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] s = in.nextLine().split(",");
Arrays.sort(s);
int l = s.length;
for (int i = 0; i < l - 1; i++) {
System.out.print(s[i] + ",");
}
System.out.println(s[l-1]);
}
}
}
K.多组空格分隔的两个正整数
输入有多组测试用例,每组空格隔开两个整数
对于每组数据输出一行两个整数的和
1 1
2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLong()) { // 注意 while 处理多个 case
Long a = in.nextLong();
Long b = in.nextLong();
System.out.println(a + b);
}
}
}
Scanner 类是获取键盘输入的一个类,首先先创建 Scanner 对象
Scanner sc = new Scanner(System.in);
接下来通过Scanner 类的方法来获取输入,在调用方法之前一般可以采取has…方法判断是否有输入。
next 和 nextLine 都是获取输入字符串的方法
next( )方法 | nextLine( )方法 |
---|---|
只能读取到空格之前的字符串 | 可以读取空格的字符串 |
比如“你好 java”,只能读取“你好” | 比如“你好 java”,可以读取“你好 java” |
在读取前可以使用 hasNext 与 hasNextLine 判断是否有输入的数据
if ( sc.hasNext()) { String str1=sc.next(); }
if ( sc.hasNextLine()) { String str2=sc.nextLine(); }
还可以接受整数和小数
i = scan.nextInt(); // 接收整数
f = scan.nextFloat(); // 接收小数
Integer.parseInt和Integer.valueOf的区别
parseInt( )://返回的是基本类型int
valueOf( )://返回的是包装类Integer