From ad1bb377bdc4740e7f1917f0370a3dceb723808a Mon Sep 17 00:00:00 2001 From: htabuchi Date: Wed, 2 Jul 2025 17:51:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=80=9Cpaiza=E3=81=AE=E5=95=8F=E9=A1=8CB1?= =?UTF-8?q?31=E3=81=AE=E5=9B=9E=E7=AD=94=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htabuchi/src/B131_htabuchi.java | 136 ++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 htabuchi/src/B131_htabuchi.java diff --git a/htabuchi/src/B131_htabuchi.java b/htabuchi/src/B131_htabuchi.java new file mode 100644 index 0000000..3a5ae6f --- /dev/null +++ b/htabuchi/src/B131_htabuchi.java @@ -0,0 +1,136 @@ +/** + * Paiza問題B131:運賃計算
+ * create 2025/07/02 + * + * @author 田渕日奈子 + * @version 1.0 + */ + +import java.util.Scanner; + +public class B131_htabuchi { + final static int MatrixColumn = 2; + + public static Dto scanningRequiredValue() { + /** 路線や駅、その金額を読み取り */ + Scanner sc = new Scanner(System.in); + int lineTrain = sc.nextInt(); /** 路線 */ + int numStation = sc.nextInt(); /** 駅数 */ + int[][] allPrice = new int[lineTrain][numStation]; + + /** 路線ごとの金額一覧(注意:最初の駅1から任意の駅nまでの値段) */ + for (int i = 0; i < lineTrain; i++) { + for (int j = 0; j < numStation; j++) { + allPrice[i][j] = sc.nextInt(); + } + } + + int requiredRoute = sc.nextInt(); + int[][] requiredRouteMatrix = new int[requiredRoute][MatrixColumn]; + + /** 通る路線と駅を格納 */ + for (int i = 0; i < requiredRoute; i++) { + for (int j = 0; j < MatrixColumn; j++) { + requiredRouteMatrix[i][j] = sc.nextInt(); + } + } + + Dto dto = new Dto(allPrice, requiredRouteMatrix, requiredRoute); + + sc.close(); + return dto; + } + + private static class Dto { + /** ルートや路線、駅、金額の読みだし・書き出し */ + private int[][] allPrice; + /** すべての金額表 */ + private int[][] requiredRouteMatrix; + /** 通る路線(row)と駅(column) */ + private int requiredRoute; + + /** 通るべき駅の数 */ + + public Dto(final int[][] allPrice, final int[][] requiredRouteMatrix, final int requiredRoute) { + this.allPrice = allPrice; + this.requiredRouteMatrix = requiredRouteMatrix; + this.requiredRoute = requiredRoute; + } + + /** 以下ゲッターとセッター */ + public int getRequiredRoute() { + return requiredRoute; + } + + public int getRequiredRouteMatrix(int row, int column) { + return requiredRouteMatrix[row][column]; + } + + public int getAllPrice(int row, int column) { + return allPrice[row][column]; + } + + } + + public static int judgePrice(Dto dto) { + int curLine = 0; /** 現在の路線 */ + int curStation = 0; /** 現在の駅 */ + int sumPrice = 0; + + for (int i = 0; i < dto.getRequiredRoute(); i++) { + /** 目指すべき駅の場所をallPrice配列用に置き換える */ + int nextLine = dto.getRequiredRouteMatrix(i, 0) - 1; + int nextStation = dto.getRequiredRouteMatrix(i, 1) - 1; + + /** 今の路線が次の目的の路線とあっているか確認 */ + if (nextLine == curLine) { + /** 変更なし */ + } else { + /** 同じ駅で路線変更 */ + curLine = nextLine; + } + /** 今の駅の位置と目的の駅の位置から金額算出 */ + if (curStation == nextStation) { + /** 同じ駅は金額0 */ + } else if (curStation < nextStation) { + /** 今の駅よりも目的の駅が先 */ + sumPrice = culcSectionFoward(dto, curLine, curStation, nextLine, nextStation, sumPrice); + curStation = nextStation; + } else { + /** 今の駅よりも目的の駅が後ろ */ + sumPrice = culcSectionBack(dto, curLine, curStation, nextLine, nextStation, sumPrice); + curStation = nextStation; + } + + } + + return sumPrice; + } + + public static int culcSectionFoward(Dto dto, int curLine, int curStation, int nextLine, + int nextStation, int sumPrice) { + int sectionPrice = dto.getAllPrice(nextLine, nextStation) - dto + .getAllPrice(curLine, curStation); + sumPrice += sectionPrice; + return sumPrice; + } + + public static int culcSectionBack(Dto dto, int curLine, int curStation, int nextLine, + int nextStation, int sumPrice) { + int sectionPrice = dto.getAllPrice(curLine, curStation) - dto + .getAllPrice(nextLine, nextStation); + sumPrice += sectionPrice; + return sumPrice; + } + + public static void displayResultPrice(int sumPrice) { + /** 表示 */ + System.out.println(sumPrice); + } + + public static void main(String[] args) { + Dto dto = scanningRequiredValue(); + int sumPrice = judgePrice(dto); + displayResultPrice(sumPrice); + } +} -- GitLab From 7885cf705c5566ff596ad118bebd02790c9ac54b Mon Sep 17 00:00:00 2001 From: htabuchi Date: Thu, 3 Jul 2025 17:32:47 +0900 Subject: [PATCH 2/2] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB131?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htabuchi/src/B131_htabuchi.java | 108 +++++++++++++++++++------------- 1 file changed, 66 insertions(+), 42 deletions(-) diff --git a/htabuchi/src/B131_htabuchi.java b/htabuchi/src/B131_htabuchi.java index 3a5ae6f..741835f 100644 --- a/htabuchi/src/B131_htabuchi.java +++ b/htabuchi/src/B131_htabuchi.java @@ -6,54 +6,77 @@ * @version 1.0 */ +import java.util.ArrayList; +import java.util.List; import java.util.Scanner; public class B131_htabuchi { final static int MatrixColumn = 2; - public static Dto scanningRequiredValue() { + public TrainMapData scanningRequiredValue() { /** 路線や駅、その金額を読み取り */ Scanner sc = new Scanner(System.in); int lineTrain = sc.nextInt(); /** 路線 */ int numStation = sc.nextInt(); /** 駅数 */ - int[][] allPrice = new int[lineTrain][numStation]; + List allPriceList = new ArrayList<>(); /** 路線ごとの金額一覧(注意:最初の駅1から任意の駅nまでの値段) */ - for (int i = 0; i < lineTrain; i++) { - for (int j = 0; j < numStation; j++) { - allPrice[i][j] = sc.nextInt(); + for (int line = 0; line < lineTrain; line++) { + List fee = new ArrayList<>(); + for (int column = 0; column < numStation; column++) { + fee.add(sc.nextInt()); } + /** 路線コード、各駅の運賃を格納 */ + allPriceList.add(new AllLineInfo(line, fee)); } int requiredRoute = sc.nextInt(); - int[][] requiredRouteMatrix = new int[requiredRoute][MatrixColumn]; + List requiredRouteList = new ArrayList<>(); /** 通る路線と駅を格納 */ - for (int i = 0; i < requiredRoute; i++) { - for (int j = 0; j < MatrixColumn; j++) { - requiredRouteMatrix[i][j] = sc.nextInt(); - } + for (int route = 0; route < requiredRoute; route++) { + int nextLine = sc.nextInt(); + int nextStation = sc.nextInt(); + requiredRouteList.add(new int[] { + nextLine, nextStation + }); } - Dto dto = new Dto(allPrice, requiredRouteMatrix, requiredRoute); + TrainMapData dto = new TrainMapData(allPriceList, requiredRouteList, requiredRoute); sc.close(); return dto; } - private static class Dto { - /** ルートや路線、駅、金額の読みだし・書き出し */ - private int[][] allPrice; + /** 路線ごとの情報を格納する */ + private class AllLineInfo { + private int code; + private List fee; + + public AllLineInfo(int code, List fee) { + this.code = code; + this.fee = fee; + } + + /** 目的の区間の金額を算出 */ + public int getSectionPrice(int forwardStation, int backStation) { + return fee.get(forwardStation) - fee.get(backStation); + } + } + + /** AllLineInfo(路線ごとの情報)含めた経路や駅、金額の情報を格納 */ + private class TrainMapData { /** すべての金額表 */ - private int[][] requiredRouteMatrix; + private List allPriceList; /** 通る路線(row)と駅(column) */ - private int requiredRoute; - + private List requiredRouteList; /** 通るべき駅の数 */ + private int requiredRoute; - public Dto(final int[][] allPrice, final int[][] requiredRouteMatrix, final int requiredRoute) { - this.allPrice = allPrice; - this.requiredRouteMatrix = requiredRouteMatrix; + public TrainMapData(final List allPriceList, final List requiredRouteList, + final int requiredRoute) { + this.allPriceList = allPriceList; + this.requiredRouteList = requiredRouteList; this.requiredRoute = requiredRoute; } @@ -62,25 +85,27 @@ public class B131_htabuchi { return requiredRoute; } - public int getRequiredRouteMatrix(int row, int column) { - return requiredRouteMatrix[row][column]; + public int[] getRequiredRouteList(int num) { + return requiredRouteList.get(num); } - public int getAllPrice(int row, int column) { - return allPrice[row][column]; + public AllLineInfo getAllPriceList(int line) { + return allPriceList.get(line); } } - public static int judgePrice(Dto dto) { + /** 目的の経路までにかかる金額の算出 */ + public static int judgePrice(TrainMapData dto) { int curLine = 0; /** 現在の路線 */ int curStation = 0; /** 現在の駅 */ int sumPrice = 0; for (int i = 0; i < dto.getRequiredRoute(); i++) { /** 目指すべき駅の場所をallPrice配列用に置き換える */ - int nextLine = dto.getRequiredRouteMatrix(i, 0) - 1; - int nextStation = dto.getRequiredRouteMatrix(i, 1) - 1; + int[] nextInfo = dto.getRequiredRouteList(i); + int nextLine = nextInfo[0] - 1; + int nextStation = nextInfo[1] - 1; /** 今の路線が次の目的の路線とあっているか確認 */ if (nextLine == curLine) { @@ -94,11 +119,11 @@ public class B131_htabuchi { /** 同じ駅は金額0 */ } else if (curStation < nextStation) { /** 今の駅よりも目的の駅が先 */ - sumPrice = culcSectionFoward(dto, curLine, curStation, nextLine, nextStation, sumPrice); + sumPrice += culcSectionFoward(dto, curLine, curStation, nextLine, nextStation); curStation = nextStation; } else { /** 今の駅よりも目的の駅が後ろ */ - sumPrice = culcSectionBack(dto, curLine, curStation, nextLine, nextStation, sumPrice); + sumPrice += culcSectionBack(dto, curLine, curStation, nextLine, nextStation); curStation = nextStation; } @@ -107,20 +132,18 @@ public class B131_htabuchi { return sumPrice; } - public static int culcSectionFoward(Dto dto, int curLine, int curStation, int nextLine, - int nextStation, int sumPrice) { - int sectionPrice = dto.getAllPrice(nextLine, nextStation) - dto - .getAllPrice(curLine, curStation); - sumPrice += sectionPrice; - return sumPrice; + /** 区間ごとの金額を算出する */ + /** パターン1:目的駅が現在駅よりも前 パターン2:目的駅が現在駅よりも後ろ */ + public static int culcSectionFoward(TrainMapData dto, int curLine, int curStation, int nextLine, + int nextStation) { + AllLineInfo LineInfo = dto.getAllPriceList(curLine); /** 今の路線の金額情報獲得 */ + return LineInfo.getSectionPrice(nextStation, curStation); } - public static int culcSectionBack(Dto dto, int curLine, int curStation, int nextLine, - int nextStation, int sumPrice) { - int sectionPrice = dto.getAllPrice(curLine, curStation) - dto - .getAllPrice(nextLine, nextStation); - sumPrice += sectionPrice; - return sumPrice; + public static int culcSectionBack(TrainMapData dto, int curLine, int curStation, int nextLine, + int nextStation) { + AllLineInfo LineInfo = dto.getAllPriceList(curLine); /** 今の路線の金額情報獲得 */ + return LineInfo.getSectionPrice(curStation, nextStation); } public static void displayResultPrice(int sumPrice) { @@ -129,7 +152,8 @@ public class B131_htabuchi { } public static void main(String[] args) { - Dto dto = scanningRequiredValue(); + B131_htabuchi program = new B131_htabuchi(); + TrainMapData dto = program.scanningRequiredValue(); int sumPrice = judgePrice(dto); displayResultPrice(sumPrice); } -- GitLab