Blue Bridge Cup Wrong Bill Problem (Java Solution)

topic

Insert picture description here

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

Insert picture description here

Recommended by other articles in this blog

(Recursive method) Do not build a binary tree model to solve the problem of "write out the post-order sequence according to the pre-order sequence and middle-order sequence"

The half-set problem of algorithm design and analysis

Record about the automatic initialization of C++ integer array to zero

Linear time selection for algorithm design and analysis

Guess you like

Origin blog.csdn.net/L333333333/article/details/102882042