分析:
本题较难的地方在于对录入的字符串的接收,在接收完数字N之后,要用scanner.nextLine();将换行符“吃掉”。先用字符串来接收整行的数据,然后将其用空格分割开,用字符串数组来存储分割后的字符串。用Integer.parseInt()方法将数据由字符串转化为int型数。由于数字的个数是不确定的,我们可以用List集合来存储数据。
然后用Collections.sort()方法对List集合中的数据进行排序,数据有序之后,如果相邻两个数据之差等于2,则这两个数据是直接重号了。如果相邻两个数据之差等于0,则这两个数据是重号的。
Java代码实现:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
int N = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < N; i++) {
String line = scanner.nextLine();
String [] split = line.split("