1
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?

スーパースーパースーパーカー

Posted at

今日もクラスの問題で、「スーパースーパースーパーカー」に挑戦!

なんと問題のランクが A でビビってたけど昨日より簡単だった✌️

継承の super() とスーパーカーの ”スーパー” がかかってるのかな?


🚗 問題概要

  • N 台の車がある。それぞれ燃料量 l と燃費 f が設定されている。
  • 車には 3 種類あり、車種ごとに使える機能が異なる。

🚙 車種と機能

✅ スーパーカー

  • run
    • 燃料を 1 消費して f km 走る
    • 燃料が 0 なら何もしない

✅ スーパースーパーカー

  • run はスーパーカーと同じ
  • fly
    • 燃料を 5 消費して f² km 飛ぶ
    • 燃料が 5 未満の場合は代わりに run を行う

✅ スーパースーパースーパーカー

  • run はスーパーカーと同じ
  • fly
    • 燃料を 5 消費して 2 × f² km 飛ぶ
    • 燃料が 5 未満の場合は代わりに run を行う
  • teleport
    • 燃料を f² 消費して f⁴ km 瞬間移動
    • 燃料が f² 未満の場合は代わりに fly を行う

📋 入力

  • N:車の台数
  • K:呼び出される機能の回数
  • 各車の「車種」「燃料 l」「燃費 f」
  • K 回分の「車番号」「使う機能名」

🎯 出力

すべての操作が終わった後、各車の 総移動距離 を 1 台ずつ出力する


⚠️ 前提条件

  • 燃料が足りない場合の動作(代替の run または fly)を正しく行うこと
  • 未定義の機能が呼ばれることはない(入力が正しいことは保証されている)



入力例:

3 6
supercar 1 1
supersupercar 10 10
supersupersupercar 100 5
1 run
2 run
2 fly
3 run
3 fly
3 teleport

出力例:

1
110
680







✅ OKコード例:

class SuperCar {
    constructor(l, f){
        // 燃料の量 l と燃費 f 
        this.l = Number(l);
        this.f = Number(f);
        this.totalMileage = 0;
    }
    getTotalMileage(){
        return this.totalMileage;
    }
    run(){
        if(this.l >0){
            this.l--;
            this.totalMileage += this.f;
        }
    }
}

class SuperSuperCar extends SuperCar{
    fly(){
        if(this.l >= 5){
            this.l -= 5;
            this.totalMileage += this.f ** 2;
        }
        else{
            super.run();
        }
    }
}

class SuperSuperSuperCar extends SuperCar{
    fly(){
        if(this.l >= 5){
            this.l -= 5;
            this.totalMileage += 2 * this.f ** 2;
        }
        else{
            super.run();
        }
    }
    teleport(){
        if(this.l >= this.f * this.f){
            this.l -= this.f * this.f;
            this.totalMileage += this.f ** 4;
        }
        else{
            this.fly();
        }
    }
}


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

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


rl.on('close', () => {
    const [N, K] = lines[0].split(' ').map(Number);
    
    const cars = lines.slice(1, N+1).map(c => {
        const [model, l, f] = c.split(' ');
        if(model === 'supercar') return new SuperCar(l, f);
        if(model === 'supersupercar') return new SuperSuperCar(l, f);
        if(model === 'supersupersupercar') return new SuperSuperSuperCar(l, f);
    });
    
    for(let i = N+1; i < 1 + N + K; i++){
        const [n, func] = lines[i].split(' ');
        const carNum = Number(n) - 1;
        
        cars[carNum][func]();
    }
    
    cars.forEach(c => console.log(c.getTotalMileage()));
});

✅ クラス設計のポイント

  • 全ての車には共通して
    • 燃料の量
    • 燃費
    • 総移動距離
    • 走る機能(run)
      が必要。

  • これらの共通機能・共通データは
    → 基底クラス(スーパーカー) にまとめる。

  • スーパースーパーカー や スーパースーパースーパーカー は
    • スーパーカーの特徴をそのまま引き継ぐ
    • 追加の機能(fly や teleport)だけを拡張する。

  • 継承を使うことで、共通部分を何度も書かずに済み
    → プログラムが シンプルで保守しやすい ものになる。





🗒️ メモ&まとめ

  • 共通する性質や動作は基底クラスにまとめる
  • 追加機能だけ子クラスで拡張する
  • これにより、重複を避けて無駄なく実装できる!




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

1
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
1
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?