From ea3fad3b21a4cff3be0a99142edf8e4d10f28785 Mon Sep 17 00:00:00 2001 From: rtanaka Date: Mon, 7 Jul 2025 15:49:11 +0900 Subject: [PATCH] =?UTF-8?q?paiza=E3=81=AE=E5=95=8F=E9=A1=8CB131=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 --- rtanaka/src/B131.java | 53 +++++++++++++++++ rtanaka/src/FareCalculation.java | 57 +++++++++++++++++++ ...5\345\207\272\345\212\233\344\276\213.txt" | 46 +++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 rtanaka/src/B131.java create mode 100644 rtanaka/src/FareCalculation.java create mode 100644 "rtanaka/src/\345\205\245\345\207\272\345\212\233\344\276\213.txt" diff --git a/rtanaka/src/B131.java b/rtanaka/src/B131.java new file mode 100644 index 0000000..372d9d7 --- /dev/null +++ b/rtanaka/src/B131.java @@ -0,0 +1,53 @@ + +/** + * Paiza問題B131:運賃計算 + * create 2025/07/07 + * メインファイル + * @author 田中亮汰 + * @version 1.0 + */ + +import java.util.Scanner; + +public class B131 { + + /** + * @param TO_STATION_NUM 経由座標を示すための定数 + * @param TRAIN_LINE_NUM 路線の最大数を示す定数 + * @param STATION_NUM 駅の最大数を示す定数 + */ + public static void main(String[] args) { + /* 入力 */ + Scanner sc = new Scanner(System.in); + // 路線数と駅数の取得 + final int TO_STATION_NUM = 2; + final int TRAIN_LINE_NUM = sc.nextInt(); + final int STATION_NUM = sc.nextInt(); + + // 運賃表の取得 + int[][] fares = new int[TRAIN_LINE_NUM][STATION_NUM]; + for (int i = 0; i < TRAIN_LINE_NUM; i++) { + for (int j = 0; j < STATION_NUM; j++) { + fares[i][j] = sc.nextInt(); + } + } + + // 経由駅数の取得 + int TRANSIT_STATION_NUM = sc.nextInt(); + + // 経由ルートの取得 + int[][] routes = new int[TRANSIT_STATION_NUM][TO_STATION_NUM]; + for (int i = 0; i < TRANSIT_STATION_NUM; i++) { + routes[i][0] = sc.nextInt();// 乗り換え路線番号 + routes[i][1] = sc.nextInt();// 乗り換え駅番号 + } + sc.close(); + + /* 計算 */ + FareCalculation calculate = new FareCalculation(fares, routes); + int totalFare = calculate.calculateTotalFare(); + + /* 出力 */ + System.out.println(totalFare); + } +} diff --git a/rtanaka/src/FareCalculation.java b/rtanaka/src/FareCalculation.java new file mode 100644 index 0000000..46d30e6 --- /dev/null +++ b/rtanaka/src/FareCalculation.java @@ -0,0 +1,57 @@ +/** + * Paiza問題B131:運賃計算 + * create 2025/07/07 + * 運賃計算クラス + * @author 田中亮汰 + * @version 1.0 + */ + +/** + * @param fares 各路線の運賃表 + * @param routes 経由する駅のルート情報 + * @param transitStationNum 経由する駅の数 + */ +class FareCalculation { + private int[][] fares; + private int[][] routes; + private int transitStationNum; + + public FareCalculation(int[][] fares, int[][] routes) { + this.fares = fares; + this.routes = routes; + this.transitStationNum = routes.length; // 経由駅の数はルート情報の配列の長さに等しい + } + + /**合計運賃を計算 + * @param totalFare 合計運賃を保持 + * @param currentRoute 現在の路線を保持 + * @param currentStation 現在の駅を保持 + * + * targetに対しての-1は配列の添え字としての役割を持つため + * @param targetRoute 目的の路線を保持 + * @param targetStation 目的の駅を保持 + */ + public int calculateTotalFare() { + int totalFare = 0; + int currentRoute = 0; + int currentStation = 0; + + for (int i = 0; i < transitStationNum; i++) { + // ターゲットとなる駅の路線番号と駅番号を取得 + int targetRoute = routes[i][0] - 1; + int targetStation = routes[i][1] - 1; + + // 目的駅番号と現在駅番号が違うときに運賃を加算 + if (currentStation != targetStation) { + int fareSegment = Math.abs(fares[targetRoute][targetStation] + - fares[targetRoute][currentStation]); + totalFare += fareSegment; + } + + // 現在地を更新 + currentRoute = targetRoute; + currentStation = targetStation; + } + return totalFare; + } +} \ No newline at end of file diff --git "a/rtanaka/src/\345\205\245\345\207\272\345\212\233\344\276\213.txt" "b/rtanaka/src/\345\205\245\345\207\272\345\212\233\344\276\213.txt" new file mode 100644 index 0000000..41062e9 --- /dev/null +++ "b/rtanaka/src/\345\205\245\345\207\272\345\212\233\344\276\213.txt" @@ -0,0 +1,46 @@ +/*入力例1*/ +3 4 +0 1 2 3 +0 4 5 6 +0 7 8 9 +3 +1 4 +3 2 +2 2 +/*出力例1*/ +5 + +/*入力例2*/ +1 9 +0 2 7 10 13 33 74 76 82 +4 +1 6 +1 4 +1 5 +1 3 +/*出力例2*/ +65 + +/*入力例3*/ +8 9 +0 1 3 4 22 34 60 63 85 +0 35 44 50 53 56 61 90 99 +0 7 15 35 42 51 53 96 100 +0 23 40 41 44 64 67 83 91 +0 5 36 47 59 70 81 87 97 +0 5 11 14 30 34 64 76 90 +0 4 5 22 57 62 90 91 98 +0 8 13 25 35 41 60 68 82 +10 +2 6 +3 4 +6 1 +4 4 +5 4 +8 6 +1 8 +5 6 +4 6 +5 1 +/*出力例3*/ +259 -- GitLab