From 423cc2851f2695db8ee10a0d3ff59885001e7c56 Mon Sep 17 00:00:00 2001 From: rooki Date: Tue, 8 Jul 2025 13:38:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB117=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B117.java | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 rooki/B117.java diff --git a/rooki/B117.java b/rooki/B117.java new file mode 100644 index 0000000..f055d8a --- /dev/null +++ b/rooki/B117.java @@ -0,0 +1,72 @@ +import java.util.Scanner; + +/** + * 教習車が何周するかカウント. + * + * @author rooki + * @version 1.0 + */ + +public class B117 { + + /** + * mainメソッド. + * + * @param args 使用しない + */ + public static void main(final String[] args) { + final Scanner sc = new Scanner(System.in); + + // 車の並び方の情報を保存 + final CarInfo carOrder = new CarInfo(sc); + + roundCalculation(carOrder); + + sc.close(); + } + + /** + * 車が何周しなければならないか計算する. + * + * @param carOrder 車の並び方の情報が保存されている + */ + public static void roundCalculation(CarInfo carOrder) { + int roundCount = 0; + int carNumber = 1; + + LABEL: while (true) { + // 出口に近い車から順に該当する車番かどうかチェック + for (int nearToExit = 0; nearToExit < carOrder.carCount; nearToExit++) { + // 該当の車番があれば次の車番へ移る + if (carOrder.carMap[nearToExit] == carNumber) { + carNumber += 1; + // 車番が車の台数を超えたらループから抜ける + if (carNumber > carOrder.carCount) { + break LABEL; + } + } + } + roundCount += 1; + } + System.out.println(roundCount); + } + + /** + * 車の情報を持つクラス. + * + */ + + static class CarInfo { + private int carCount; + private int[] carMap; + + public CarInfo(final Scanner sc) { + this.carCount = sc.nextInt(); + this.carMap = new int[carCount]; + + for (int i = 0; i < carCount; i++) { + carMap[i] = sc.nextInt(); + } + } + } +} -- GitLab From a41d2b437f1a47c9f767596cca716bd74025d610 Mon Sep 17 00:00:00 2001 From: rooki Date: Tue, 8 Jul 2025 15:21:38 +0900 Subject: [PATCH 2/2] =?UTF-8?q?paiza=20=E5=95=8F=E9=A1=8CB117=E3=81=AE?= =?UTF-8?q?=E3=83=AC=E3=83=93=E3=83=A5=E3=83=BC=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rooki/B117.java | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/rooki/B117.java b/rooki/B117.java index f055d8a..c25bf76 100644 --- a/rooki/B117.java +++ b/rooki/B117.java @@ -6,7 +6,6 @@ import java.util.Scanner; * @author rooki * @version 1.0 */ - public class B117 { /** @@ -20,7 +19,9 @@ public class B117 { // 車の並び方の情報を保存 final CarInfo carOrder = new CarInfo(sc); - roundCalculation(carOrder); + final int roundCount = roundCalculation(carOrder); + + outputResult(roundCount); sc.close(); } @@ -29,25 +30,36 @@ public class B117 { * 車が何周しなければならないか計算する. * * @param carOrder 車の並び方の情報が保存されている + * @return 何周したのかを返す */ - public static void roundCalculation(CarInfo carOrder) { + public static int roundCalculation(CarInfo carOrder) { int roundCount = 0; int carNumber = 1; - LABEL: while (true) { + while (true) { // 出口に近い車から順に該当する車番かどうかチェック for (int nearToExit = 0; nearToExit < carOrder.carCount; nearToExit++) { // 該当の車番があれば次の車番へ移る - if (carOrder.carMap[nearToExit] == carNumber) { + if (carOrder.rowOfCar[nearToExit] == carNumber) { carNumber += 1; - // 車番が車の台数を超えたらループから抜ける - if (carNumber > carOrder.carCount) { - break LABEL; - } } } - roundCount += 1; + // 車番が車の台数を超えたらループから抜ける + if (carNumber > carOrder.carCount) { + break; + } else { + roundCount += 1; + } } + return (roundCount); + } + + /** + * 何周したのかを出力する. + * + * @param roundCount 何周したのか保存されている + */ + public static void outputResult(final int roundCount) { System.out.println(roundCount); } @@ -55,17 +67,16 @@ public class B117 { * 車の情報を持つクラス. * */ - static class CarInfo { private int carCount; - private int[] carMap; + private int[] rowOfCar; public CarInfo(final Scanner sc) { this.carCount = sc.nextInt(); - this.carMap = new int[carCount]; + this.rowOfCar = new int[carCount]; for (int i = 0; i < carCount; i++) { - carMap[i] = sc.nextInt(); + rowOfCar[i] = sc.nextInt(); } } } -- GitLab