diff --git a/syamauchi/src/B068.java b/syamauchi/src/B068.java new file mode 100644 index 0000000000000000000000000000000000000000..bd5be55ff63801980f76a6c3a4f9cdc7b1ed1924 --- /dev/null +++ b/syamauchi/src/B068.java @@ -0,0 +1,97 @@ +package src; + +import java.util.Scanner; + +public class B068 { + + final String[] DIVIDE_PEOPLE = { + "A", "B" + }; + + public static void main(String[] args) { + final Scanner sc = new Scanner(System.in); + new B068().chocolateSplit(sc); + } + + private void chocolateSplit(final Scanner sc) { + final int[][] chocolateList = chocolateContains(sc); + if (canChocolateBisection(chocolateList)) { + divideSugarEqually(chocolateList); + } else { + System.out.println("No"); + } + + } + + // チョコのリストを作成 + private int[][] chocolateContains(final Scanner sc) { + final int[][] chocolateList = new int[sc.nextInt()][sc.nextInt()]; + for (int i = 0; i < chocolateList.length; i++) { + for (int j = 0; j < chocolateList[i].length; j++) { + chocolateList[i][j] = sc.nextInt(); + } + } + return chocolateList; + } + + // すべての行で糖度が等分できるか + private boolean canChocolateBisection(final int[][] chocolateList) { + for (int i = 0; i < chocolateList.length; i++) { + if (!canSugerHarf(totalSugar(chocolateList, i))) { + return false; + } + } + return true; + } + + // 糖度が等分できるか + private boolean canSugerHarf(final int totalSugar) { + return totalSugar % DIVIDE_PEOPLE.length == 0; + } + + // 振り分け結果を出力 + private void showChocolate(final String[][] dividedChocolate) { + for (String[] divideList : dividedChocolate) { + for (String value : divideList) { + System.out.print(value); + } + System.out.println(); + } + } + + // 任意の行の糖度の合計を計算 + private int totalSugar(final int[][] chocolateList, final int height) { + int totalSugar = 0; + for (int i = 0; i < chocolateList[height].length; i++) { + totalSugar += chocolateList[height][i]; + } + return totalSugar; + } + + // 糖度が同じになるように分ける + private void divideSugarEqually(final int[][] chocolateList) { + final String[][] dividedChocolete = new String[chocolateList.length][chocolateList[0].length]; + for (int i = 0; i < chocolateList.length; i++) { + + final int[] recieveSugar = new int[DIVIDE_PEOPLE.length]; + int peopleCount = 0; + final int divideSugar = totalSugar(chocolateList, i) / DIVIDE_PEOPLE.length; + for (int j = 0; j < chocolateList[i].length; j++) { + dividedChocolete[i][j] = DIVIDE_PEOPLE[peopleCount]; + recieveSugar[peopleCount] += chocolateList[i][j]; + // 次の人にチョコを割り振る + if (recieveSugar[peopleCount] == divideSugar) { + peopleCount++; + } else if (recieveSugar[peopleCount] > divideSugar) { + System.out.println("No"); + return; + } + } + } + + System.out.println("Yes"); + + showChocolate(dividedChocolete); + } + +}