Wrong ticket problem
topic
Code reference passed by me
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
private static List<Integer> ids = new ArrayList<Integer>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
int i = 0;
while (sc.hasNextLine()) {
String[] temp = sc.nextLine().trim().replaceAll("\\s+", ",").split(",");
for (String id : temp) {
ids.add(Integer.parseInt(id));
}
if (++i >= N)
break;
}
ids.sort(null);
int m = 0, n = 0;
for (int j = 1; j < ids.size() - 1; j++) {
int front = ids.get(j);
int rear = ids.get(j + 1);
if (front == rear)
n = front;
else if (front != rear - 1)
m = rear - 1;
if (m != 0 && n != 0)
break;
}
System.out.println(m + " " + n);
sc.close();
}
}
Test a set of sample data
Recommended by other articles in this blog
The half-set problem of algorithm design and analysis
Record about the automatic initialization of C++ integer array to zero