题解:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr = new int[100005];
Scanner in = new Scanner(System.in);
int N = Integer.parseInt(in.nextLine());
//此处不能用in.nextIN(),否则下面的nextLine会读入上面一行结尾的回车符
int min = 100005, max = 0;
while (N != 0) {
N--;
String[] str = in.nextLine().split("\\s+");
//split里的正则表达式可以将一个字符串拆穿成一个字符数组
//这里的\\s表示若干空格
for (int i = 0; i < str.length; i++) {
int index = Integer.parseInt(str[i]);
//静态方法pareseInt可以将字符串转换为数字
arr[index]++;
min = Math.min(min, index);
max = Math.max(max, index);
}
}
int n = 0, m = 0;
for (int i = min; i <= max; i++) {
if (arr[i] == 0)
m = i;
if (arr[i] == 2)
n = i;
}
System.out.println(m + " " + n);
in.close();
}
}
也可以使用BufferedReader(效率更高,推荐):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class No_1204 {
public static void main(String[] args) throws IOException {
int[] arr = new int[100005];
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bf.readLine());
int min = 100005, max = 0;
while (N != 0) {
N--;
String[] str = bf.readLine().split("\\s+");
for (int i = 0; i < str.length; i++) {
int index = Integer.parseInt(str[i]);
arr[index]++;
min = Math.min(min, index);
max = Math.max(max, index);
}
}
int n = 0, m = 0;
for (int i = min; i <= max; i++) {
if (arr[i] == 0)
m = i;
if (arr[i] == 2)
n = i;
}
System.out.println(m + " " + n);
bf.close();
}
}