ABC093 C --Same Integers

Given three integers, the problem of finding how many operations are required to make the same number according to the two methods. I couldn't understand the explanation of the model answer even after reading it, so I solved it with my own solution.

solution

Put the three numbers in an array and sort them in ascending order. Example) 4,8,5 With the Arrays.sort method It changes in the order of 4, 5, 8. While array [0] <array [1], add +2 to array [0]. When array [0] == array [1], array [0] ++, array [1] ++ is performed. Also, return to the beginning of the while statement and execute the Array.sort method. In the case of this example array [0] is incremented by +2 to 6,5,8, Depending on the method, the order is 5,6,8. A solution can be obtained by repeating this many times. However, I can't prove why this method is the shortest, so if anyone knows, please leave it in the comments.

import java.util.Arrays; import java.util.Scanner; public class Main{

public static void main(String[] args) {
	Scanner stdIn = new Scanner(System.in);
	int [] array = new int[3];
	array[0] = stdIn.nextInt();
	array[1] = stdIn.nextInt();
	array[2] = stdIn.nextInt(); 
	int cnt = 0;
	
	while(true) {
		
		Arrays.sort(array);
		
		if(array[0] == array[1] && array[1] == array[2]) {
			break;
		}
		
		if(array[0]<array[1]) {
			cnt++;
			array[0] += 2;
			continue;
		}
		if(array[0] == array[1]) {
			cnt++;
			array[0]++;
			array[1]++;
		}
	
	}
	System.out.println(cnt);
	
}

}

Recommended Posts

ABC093 C --Same Integers
ABC --013-A & B & C
ABC --023 --A & B & C
ABC --036-A & B & C
ABC --010 --A & B & C
ABC --028 --A & B & C
atcoder ABC113 C problem
ABC --015 --A & B & C
ABC --128 --A & B & C
ABC --012-A & B & C
ABC --018 --A & B & C
ABC --054 --A & B & C
ABC --017 --A & B & C
ABC --029- A & B & C
ABC --022 --A & B & C
ABC --019 --A & B & C
ABC --020 --A & B & C
ABC --030- A & B & C
ABC --127 --A & B & C
ABC --007 --A & B & C
ABC --132- A & B & C
ABC --026 --A & B & C
ABC --014- A & B & C
ABC --016 --A & B & C
ABC --011-A & B & C
ABC --031 --A & B & C
ABC --021 --A & B & C
ABC --025 --A & B & C
atcoder ABC115 C problem
ABC --024 --A & B & C
ABC --027 --A & B & C
ABC --080- A & B & C
ABC --129- A & B & C & D
ABC --133- A & B & C & D
ABC --122 --A & B & C & D
ABC --125- A & B & C & D
ABC --130- A & B & C & D
ABC --126 --A & B & C & D
ABC --134- A & B & C & D & E
ABC --131- A & B & C & D & E