hwangnk1004 / Algorithm

0 stars 0 forks source link

StepByStep #사탕 나누기 게임 #291

Open hwangnk1004 opened 4 years ago

hwangnk1004 commented 4 years ago

import java.util.Scanner;

public class Main { public static void main(String args[]) {

    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();

    int arr[] = new int[n];

    for (int i = 0; i < n; i++) {
        arr[i] = scanner.nextInt();
    }
    int count = 0;
    int x = 0;

    while (true) {

        count++;
        int avg = 0;

        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] / 2;
        }
        int add = arr[0];
        add += arr[arr.length - 1];
        if (add % 2 != 0) {
            add += 1;
        }
        for (int i = arr.length-1; i >=1; i--) {
            arr[i] += arr[i - 1];
            if (arr[i] % 2 != 0) {
                arr[i] += 1;
            }
            avg += arr[i];
        }

        arr[0] = add;
        avg += add;
        int k = 0;

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != avg / n) {
                k++;
                break;
            }
        }
        if (k == 0) {
            x = avg/n;
            break;
        }

    }

    System.out.println(count + " " +x);

}

}