0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【条件判定 1】郵便料金

Posted at

今回は paiza の「【条件判定 1】郵便料金」の問題に挑戦!


問題概要

  • 入力
    • 荷物の大きさ(縦 y、横 x、高さ h
    • 基準値 l1, l2, l3, l4, l5
    • 料金 m1, m2, m3, m4, m5, m6

  • 料金の決め方(ルールは上から順番にチェック)
    • 高さが l1 以下
      • 縦と横の最大が l2 以下 → m1
      • そうでなく、縦+横 ≤ l3m2
      • それ以外 → m3
    • 高さが l1 より大きい
      • 縦・横・高さの最大が l4 以下 → m4
      • そうでなく、縦+横+高さ ≤ l5m5
      • それ以外 → 体積 (y × x × h) × m6

  • 出力
    • 決定した料金を1行で出力。


入力例:

5 6 7
10 9 8 7 6
1 2 3 4 5 6

出力例:

1



✅ OK例:

const rl = require('readline').createInterface({ input:process.stdin });

const lines = [];

rl.on('line', (input) => lines.push(input));

rl.on('close', () => {
    const [y, x, h] = lines[0].split(' ').map(Number);
    const [l1, l2, l3, l4, l5] = lines[1].split(' ').map(Number);
    const [m1, m2, m3, m4, m5, m6] = lines[2].split(' ').map(Number);
    
    if (h <= l1) {
        if (Math.max(y, x) <= l2) {
            console.log(m1);
        } else if (y + x <= l3) {
            console.log(m2);
        } else {
            console.log(m3);
        } 
    }else {
        if (Math.max(y, x, h) <= l4) {
            console.log(m4);
        }else if (y + x + h <= l5) {
            console.log(m5)
        }else {
            console.log(y * x * h * m6)
        }
    }
});

  1. 入力を受け取る
    • 1行目:荷物の大きさ y, x, h
    • 2行目:基準値 l1, l2, l3, l4, l5
    • 3行目:料金 m1, m2, m3, m4, m5, m6
  2. 分岐 (高さの条件で2つの大きなケースに分かれる)
    • if (h <= l1) → 高さが低い場合のルール
    • else → 高さが大きい場合のルール
  3. 高さが低い場合 (h ≤ l1)
    • 縦横の最大が l2 以下なら → m1
    • そうでなく縦+横 ≤ l3 なら → m2
    • どちらでもないなら → m3
  4. 高さが大きい場合 (h > l1)
    • 縦横高さの最大が l4 以下なら → m4
    • そうでなく縦+横+高さ ≤ l5 なら → m5
    • どちらでもないなら → 体積 (y * x * h) × m6
  5. 判定結果を出力
    → どの条件に当てはまったかで料金を1行で出力

ポイント

  • 判定は 上から順番に チェックして、最初に当てはまったルールで料金が決まる。
  • 最後だけ「体積×料金」になるので特別。






🗒️ まとめ


条件分岐の優先順位が重要
  • 上から順に判定し、最初に当てはまったものが料金になる。

分岐の構造

  • 大きく2ケースに分ける (h <= l1 / h > l1)
  • その中でさらに3パターンに分岐。
    → 二段階条件分岐の練習問題。

エッジケースを意識

  • 「ちょうど等しい (<=)」ときに正しく判定できるかが重要。




僕の失敗談(´;ω;`)と解決法🐈

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?