
접근 방법
- 배열에 홀수가 있으면 +1
- 순환하며 다음 배열 값에 현재 값의 절반을 넘겨준다.
- 모든 값이 같은지 비교한다.
3-1. 같다면 count 값을 return한다.
3-2. 같지 않다면 1~3번을 반복한다.
import java.io.*;
public class Silver5_9037_TheCandyWar {
static int[] children;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
for (int i = 0; i < t; i++) {
int N = Integer.parseInt(br.readLine());
children = new int[N];
String[] input = br.readLine().split(" ");
for (int j = 0; j < N; j++) {
children[j] = Integer.parseInt(input[j]);
}
bw.write(String.valueOf(distributeCandy()));
bw.newLine();
}
bw.flush();
bw.close();
}
private static int distributeCandy() {
int count = 0;
checkOddCandy();
if(checkCandyNumber()) {
return count;
}
while (true) {
checkOddCandy();
if(checkCandyNumber()) {
return count;
}
int pass = children[0] / 2;
children[0] /= 2;
for (int i = 1; i < children.length; i++) {
children[i] /= 2;
int tmp = children[i];
children[i] += pass;
pass = tmp;
}
children[0] += pass;
count++;
}
}
private static void checkOddCandy() {
for (int i = 0; i < children.length; i++) {
if(children[i] % 2 == 1) {
children[i]++;
}
}
}
private static boolean checkCandyNumber() {
boolean confirm = true;
for (int i = 1; i < children.length; i++) {
if(children[0] != children[i]) {
confirm = false;
}
}
return confirm;
}
}