1
0

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

Last updated at Posted at 2024-03-12

ここはまあ難しいことはなかったです。
ただし、flyメソッドの仕様が車によって異なるところがミスしやすいですね。
しかも一番性能がいい車でteleportできないときに、super()ではなく、selfを選ばないと間違ってしまう点がミスしやすいなと感じました。
コマンドリストを使わない方法は以前の記事でコメントを下さった方のを参考にしました。

class Supercar():
    def __init__(self,fuel,length):
        # fuel =燃料, length=燃費, total =総移動距離
        self.fuel = int(fuel)
        self.length = int(length)
        self.total = 0
    
    def run(self):
        if self.fuel >= 1:
            self.fuel -= 1
            self.total += self.length
    
    def get_total(self):
         return self.total

class Supersupercar(Supercar):
    def __init__(self,fuel,length):
        super().__init__(fuel,length)
    
    def fly(self):
        if self.fuel >= 5:
            self.fuel -= 5
            self.total += self.length ** 2
        elif self.fuel < 5:
            super().run()
        
class Supersupersupercar(Supercar):
    def __init__(self,fuel,length):
        super().__init__(fuel,length)
 
    def fly(self):
        if self.fuel >= 5:
            self.fuel -= 5
            self.total += 2 * self.length ** 2
        elif self.fuel < 5:
            super().run()
            
    def teleport(self):
        if self.fuel >= self.length ** 2:
            self.fuel -= self.length ** 2
            self.total += self.length ** 4
        elif self.fuel < self.length ** 2:
            self.fly()

#ここからmain
N, K = map(int,input().split())
cars = [None] * N

#車情報
for i in range(N):
    car_type,*args= input().split()
    if car_type == 'supercar':
        cars[i] = Supercar(*args)
    elif car_type == 'supersupercar':
        cars[i] = Supersupercar(*args)
    elif car_type == 'supersupersupercar':
        cars[i] = Supersupersupercar(*args)

#アクション
for _ in range(K):
    number, command = input().split()
    number = int(number) - 1 
    getattr(cars[number], command)()


#結果
for car in cars:
    print(car.get_total())


1
0
4

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