From fc2bfd78a5e68883bd6db05654a677a900e4ed67 Mon Sep 17 00:00:00 2001 From: syamauchi Date: Tue, 4 Jul 2023 15:09:44 +0900 Subject: [PATCH 1/5] =?UTF-8?q?B068=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syamauchi/src/B068.java | 99 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 syamauchi/src/B068.java diff --git a/syamauchi/src/B068.java b/syamauchi/src/B068.java new file mode 100644 index 0000000..48f5c5b --- /dev/null +++ b/syamauchi/src/B068.java @@ -0,0 +1,99 @@ +package src; + +import java.util.Scanner; + +public class B068 { + private final int height; + private final int width; + + private B068(final int height, final int width) { + this.height = height; + this.width = width; + } + + public static void main(String[] args) { + final Scanner sc = new Scanner(System.in); + new B068(sc.nextInt(), sc.nextInt()).chocolateSplit(sc); + } + + private void chocolateSplit(final Scanner sc) { + final int[][] chocolateList = chocolateContents(sc); + if (isChocolateBisection(chocolateList)) { + divaidEqually(chocolateList); + } else { + System.out.println("No"); + } + + } + + private int[][] chocolateContents(final Scanner sc) { + final int[][] chocolateList = new int[this.height][this.width]; + for (int i = 0; i < this.height; i++) { + for (int j = 0; j < this.width; j++) { + chocolateList[i][j] = sc.nextInt(); + } + } + + return chocolateList; + } + + private boolean isChocolateBisection(final int[][] chocolateList) { + for (int i = 0; i < this.height; i++) { + if (!isSugerHarf(totalSugar(chocolateList, i))) { + return false; + } + } + return true; + } + + private boolean isSugerHarf(final int totalSugar) { + return totalSugar % 2 == 0; + } + + private int totalSugar(final int[][] chocolateList, final int height) { + final int[] totalSugar = new int[this.height]; + for (int i = 0; i < this.width; i++) { + totalSugar[height] += chocolateList[height][i]; + } + return totalSugar[height]; + } + + private void showChocolate(final String[][] dividedChocolate) { + for (int i = 0; i < this.height; i++) { + for (int j = 0; j < this.width; j++) { + System.out.print(dividedChocolate[i][j]); + } + System.out.println(); + } + } + + private void divaidEqually(final int[][] chocolateList) { + final String[][] dividedChocolete = new String[this.height][this.width]; + + for (int i = 0; i < this.height; i++) { + final int dividSugar = totalSugar(chocolateList, i) / 2; + int aliceChocolate = 0; + int bobChocolate = 0; + + for (int j = 0; j < this.width; j++) { + if (aliceChocolate < dividSugar) { + aliceChocolate += chocolateList[i][j]; + dividedChocolete[i][j] = "A"; + } else if (aliceChocolate >= dividSugar) { + bobChocolate += chocolateList[i][j]; + dividedChocolete[i][j] = "B"; + + } + + } + if (aliceChocolate != bobChocolate) { + System.out.println("No"); + return; + } + + } + System.out.println("Yes"); + showChocolate(dividedChocolete); + } + +} -- GitLab From 6149dda37adc7b6adf0241bb544e330011c3325b Mon Sep 17 00:00:00 2001 From: syamauchi Date: Wed, 5 Jul 2023 13:14:03 +0900 Subject: [PATCH 2/5] =?UTF-8?q?B068=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syamauchi/src/B068.java | 107 +++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 50 deletions(-) diff --git a/syamauchi/src/B068.java b/syamauchi/src/B068.java index 48f5c5b..2a3a12d 100644 --- a/syamauchi/src/B068.java +++ b/syamauchi/src/B068.java @@ -3,97 +3,104 @@ package src; import java.util.Scanner; public class B068 { - private final int height; - private final int width; - - private B068(final int height, final int width) { - this.height = height; - this.width = width; - } public static void main(String[] args) { final Scanner sc = new Scanner(System.in); - new B068(sc.nextInt(), sc.nextInt()).chocolateSplit(sc); + new B068().chocolateSplit(sc); } private void chocolateSplit(final Scanner sc) { - final int[][] chocolateList = chocolateContents(sc); - if (isChocolateBisection(chocolateList)) { - divaidEqually(chocolateList); + final int[][] chocolateList = chocolateContains(sc); + if (canChocolateBisection(chocolateList)) { + divaidSugarEqually(chocolateList); } else { System.out.println("No"); } } - private int[][] chocolateContents(final Scanner sc) { - final int[][] chocolateList = new int[this.height][this.width]; - for (int i = 0; i < this.height; i++) { - for (int j = 0; j < this.width; j++) { + // チョコのリストを作成 + 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 isChocolateBisection(final int[][] chocolateList) { - for (int i = 0; i < this.height; i++) { - if (!isSugerHarf(totalSugar(chocolateList, i))) { + // すべての行で糖度が等分できるか + 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 isSugerHarf(final int totalSugar) { - return totalSugar % 2 == 0; - } - - private int totalSugar(final int[][] chocolateList, final int height) { - final int[] totalSugar = new int[this.height]; - for (int i = 0; i < this.width; i++) { - totalSugar[height] += chocolateList[height][i]; - } - return totalSugar[height]; + // 糖度が等分できるか + private boolean canSugerHarf(final int totalSugar) { + final int numberOfPeople = 2; + return totalSugar % numberOfPeople == 0; } + // 振り分け結果を出力 private void showChocolate(final String[][] dividedChocolate) { - for (int i = 0; i < this.height; i++) { - for (int j = 0; j < this.width; j++) { - System.out.print(dividedChocolate[i][j]); + for (String[] dividList : dividedChocolate) { + for (String value : dividList) { + System.out.print(value); } System.out.println(); } } - private void divaidEqually(final int[][] chocolateList) { - final String[][] dividedChocolete = new String[this.height][this.width]; - - for (int i = 0; i < this.height; i++) { - final int dividSugar = totalSugar(chocolateList, i) / 2; - int aliceChocolate = 0; - int bobChocolate = 0; - - for (int j = 0; j < this.width; j++) { - if (aliceChocolate < dividSugar) { - aliceChocolate += chocolateList[i][j]; - dividedChocolete[i][j] = "A"; - } else if (aliceChocolate >= dividSugar) { - bobChocolate += chocolateList[i][j]; - dividedChocolete[i][j] = "B"; + // 任意の行の糖度の合計を計算 + 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 divaidSugarEqually(final int[][] chocolateList) { + final String[][] dividedChocolete = new String[chocolateList.length][chocolateList[0].length]; + final String[] dividPeople = { + "A", "B" + }; + for (int i = 0; i < chocolateList.length; i++) { + + final int[] recieveSugar = new int[dividPeople.length]; + int peopleCount = 0; + final int dividSugar = totalSugar(chocolateList, i) / dividPeople.length; + for (int j = 0; j < chocolateList[i].length; j++) { + dividedChocolete[i][j] = divideChocolate(dividPeople, recieveSugar, dividSugar); + recieveSugar[peopleCount] += chocolateList[i][j]; + if (recieveSugar[peopleCount] >= dividSugar) { + peopleCount++; } - } - if (aliceChocolate != bobChocolate) { + if (peopleCount == dividPeople.length - 1) { System.out.println("No"); return; } - } + System.out.println("Yes"); + showChocolate(dividedChocolete); } + private String divideChocolate(final String[] dividPeople, final int[] recieveSugar, + final int dividSugar) { + for (int i = 0; i < dividPeople.length; i++) { + if (recieveSugar[i] < dividSugar) { + return dividPeople[i]; + } + } + return "No"; + } } -- GitLab From 3fec351fea333535f49a0906726762d8980ed1cc Mon Sep 17 00:00:00 2001 From: syamauchi Date: Wed, 5 Jul 2023 13:27:39 +0900 Subject: [PATCH 3/5] =?UTF-8?q?B068=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syamauchi/src/B068.java | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/syamauchi/src/B068.java b/syamauchi/src/B068.java index 2a3a12d..63a8d66 100644 --- a/syamauchi/src/B068.java +++ b/syamauchi/src/B068.java @@ -12,7 +12,7 @@ public class B068 { private void chocolateSplit(final Scanner sc) { final int[][] chocolateList = chocolateContains(sc); if (canChocolateBisection(chocolateList)) { - divaidSugarEqually(chocolateList); + divideSugarEqually(chocolateList); } else { System.out.println("No"); } @@ -66,24 +66,26 @@ public class B068 { } // 糖度が同じになるように分ける - private void divaidSugarEqually(final int[][] chocolateList) { + private void divideSugarEqually(final int[][] chocolateList) { final String[][] dividedChocolete = new String[chocolateList.length][chocolateList[0].length]; - final String[] dividPeople = { + final String[] dividePeople = { "A", "B" }; for (int i = 0; i < chocolateList.length; i++) { - final int[] recieveSugar = new int[dividPeople.length]; + final int[] recieveSugar = new int[dividePeople.length]; int peopleCount = 0; - final int dividSugar = totalSugar(chocolateList, i) / dividPeople.length; + final int divideSugar = totalSugar(chocolateList, i) / dividePeople.length; for (int j = 0; j < chocolateList[i].length; j++) { - dividedChocolete[i][j] = divideChocolate(dividPeople, recieveSugar, dividSugar); + dividedChocolete[i][j] = divideChocolate(dividePeople, recieveSugar, divideSugar); recieveSugar[peopleCount] += chocolateList[i][j]; - if (recieveSugar[peopleCount] >= dividSugar) { + // 次の人にチョコを割り振る + if (recieveSugar[peopleCount] >= divideSugar) { peopleCount++; } } - if (peopleCount == dividPeople.length - 1) { + // チョコを等分で振り分けできていない + if (peopleCount == dividePeople.length - 1) { System.out.println("No"); return; } @@ -94,11 +96,12 @@ public class B068 { showChocolate(dividedChocolete); } - private String divideChocolate(final String[] dividPeople, final int[] recieveSugar, - final int dividSugar) { - for (int i = 0; i < dividPeople.length; i++) { - if (recieveSugar[i] < dividSugar) { - return dividPeople[i]; + // チョコの振り分け + private String divideChocolate(final String[] dividePeople, final int[] recieveSugar, + final int divideSugar) { + for (int i = 0; i < dividePeople.length; i++) { + if (recieveSugar[i] < divideSugar) { + return dividePeople[i]; } } return "No"; -- GitLab From 1f2b595c3e54ce2d4af6363dd7cad446e2d5bc92 Mon Sep 17 00:00:00 2001 From: syamauchi Date: Wed, 5 Jul 2023 14:05:53 +0900 Subject: [PATCH 4/5] =?UTF-8?q?B068=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syamauchi/src/B068.java | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/syamauchi/src/B068.java b/syamauchi/src/B068.java index 63a8d66..eae6398 100644 --- a/syamauchi/src/B068.java +++ b/syamauchi/src/B068.java @@ -3,6 +3,9 @@ package src; import java.util.Scanner; public class B068 { + final String[] dividePeople = { + "A", "B" + }; public static void main(String[] args) { final Scanner sc = new Scanner(System.in); @@ -42,14 +45,13 @@ public class B068 { // 糖度が等分できるか private boolean canSugerHarf(final int totalSugar) { - final int numberOfPeople = 2; - return totalSugar % numberOfPeople == 0; + return totalSugar % dividePeople.length == 0; } // 振り分け結果を出力 private void showChocolate(final String[][] dividedChocolate) { - for (String[] dividList : dividedChocolate) { - for (String value : dividList) { + for (String[] divideList : dividedChocolate) { + for (String value : divideList) { System.out.print(value); } System.out.println(); @@ -68,27 +70,22 @@ public class B068 { // 糖度が同じになるように分ける private void divideSugarEqually(final int[][] chocolateList) { final String[][] dividedChocolete = new String[chocolateList.length][chocolateList[0].length]; - final String[] dividePeople = { - "A", "B" - }; for (int i = 0; i < chocolateList.length; i++) { final int[] recieveSugar = new int[dividePeople.length]; int peopleCount = 0; final int divideSugar = totalSugar(chocolateList, i) / dividePeople.length; for (int j = 0; j < chocolateList[i].length; j++) { - dividedChocolete[i][j] = divideChocolate(dividePeople, recieveSugar, divideSugar); + dividedChocolete[i][j] = dividePeople[peopleCount]; recieveSugar[peopleCount] += chocolateList[i][j]; // 次の人にチョコを割り振る - if (recieveSugar[peopleCount] >= divideSugar) { + if (recieveSugar[peopleCount] == divideSugar) { peopleCount++; + } else if (recieveSugar[peopleCount] > divideSugar) { + System.out.println("No"); + return; } } - // チョコを等分で振り分けできていない - if (peopleCount == dividePeople.length - 1) { - System.out.println("No"); - return; - } } System.out.println("Yes"); @@ -96,14 +93,4 @@ public class B068 { showChocolate(dividedChocolete); } - // チョコの振り分け - private String divideChocolate(final String[] dividePeople, final int[] recieveSugar, - final int divideSugar) { - for (int i = 0; i < dividePeople.length; i++) { - if (recieveSugar[i] < divideSugar) { - return dividePeople[i]; - } - } - return "No"; - } } -- GitLab From e05951e7f94adea23079c3b2a614306127fb5db2 Mon Sep 17 00:00:00 2001 From: syamauchi Date: Wed, 5 Jul 2023 14:21:58 +0900 Subject: [PATCH 5/5] =?UTF-8?q?B068=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syamauchi/src/B068.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/syamauchi/src/B068.java b/syamauchi/src/B068.java index eae6398..bd5be55 100644 --- a/syamauchi/src/B068.java +++ b/syamauchi/src/B068.java @@ -3,7 +3,8 @@ package src; import java.util.Scanner; public class B068 { - final String[] dividePeople = { + + final String[] DIVIDE_PEOPLE = { "A", "B" }; @@ -45,7 +46,7 @@ public class B068 { // 糖度が等分できるか private boolean canSugerHarf(final int totalSugar) { - return totalSugar % dividePeople.length == 0; + return totalSugar % DIVIDE_PEOPLE.length == 0; } // 振り分け結果を出力 @@ -72,11 +73,11 @@ public class B068 { final String[][] dividedChocolete = new String[chocolateList.length][chocolateList[0].length]; for (int i = 0; i < chocolateList.length; i++) { - final int[] recieveSugar = new int[dividePeople.length]; + final int[] recieveSugar = new int[DIVIDE_PEOPLE.length]; int peopleCount = 0; - final int divideSugar = totalSugar(chocolateList, i) / dividePeople.length; + final int divideSugar = totalSugar(chocolateList, i) / DIVIDE_PEOPLE.length; for (int j = 0; j < chocolateList[i].length; j++) { - dividedChocolete[i][j] = dividePeople[peopleCount]; + dividedChocolete[i][j] = DIVIDE_PEOPLE[peopleCount]; recieveSugar[peopleCount] += chocolateList[i][j]; // 次の人にチョコを割り振る if (recieveSugar[peopleCount] == divideSugar) { -- GitLab