#42533: python 這樣寫應該算符合題意吧


sam851015@gmail.com (多挖鼻孔有益身心健康)

學校 : 臺中市立惠文高級中學
編號 : 277705
來源 : [123.192.228.253]
最後登入時間 :
2025-09-21 22:24:46

雖然我覺得直接用 list 或 dict 比較簡單......

gist上色版: 連結 

 

from sys import stdin


class ParkingLot:
    class Car:
        def __init__(self, brand: str, color: str) -> None:
            self.brand = brand
            self.color = color
       
        def __str__(self) -> str:
            return f'{self.brand} {self.color}'

    def __init__(self, total_cars: int) -> None:
        self.parking_lot = self._park(total_cars)

    def _park(self, _n: int) -> list:
        return [ParkingLot.Car(*stdin.readline().rstrip().split()) for _ in range(_n)]
   
    def query(self, total_command: int) -> None:
        for _ in range(total_command):
            command = stdin.readline().rstrip().split()

            if command[0] == 'brand':
                print(*[i for i in self.parking_lot if i.brand == command[1]], sep='\n')
           
            else:
                print(*[i for i in self.parking_lot if i.color == command[1]], sep='\n')
        print()


def main():
    for line in stdin:
        line = line.rstrip()
        if line == '':
            continue

        n, m = map(int, line.split())

        p = ParkingLot(n)
        p.query(m)


if __name__ == '__main__':
    main()

 

 

#53819: Re: python 這樣寫應該算符合題意吧


pofly (不挖鼻孔有害身心健康)

學校 : 不指定學校
編號 : 322682
來源 : [123.192.228.253]
最後登入時間 :
2025-10-11 19:00:08

學了 C++ ,回頭再寫一次這題

彩色版: gist

 

#include <iostream>
#include <vector>

// -----------------------------------------------------
// 宣告物件

struct Car
{
  std::string brand, color;
  friend std::ostream &operator<<(std::ostream &os, const Car &item);
};

class ParkinLot
{
private:
  int size;
  std::vector<Car> data;

public:
  ParkinLot();
  ParkinLot(int n);
  void push(Car info);
  void query(std::string &type, std::string &target);
};

// -----------------------------------------------------
// 實作方法的細節

// 重載 << 運算子,簡化輸出
std::ostream &operator<<(std::ostream &os, const Car &item)
{
  os << item.brand << ' ' << item.color << '\n';
  return os;
}

// 構造方法,無參數版
ParkinLot::ParkinLot()
{
  size = 0; // 沒用到,隨便塞一個數字,也可以不寫
}

// 構造方法,有參數版
ParkinLot::ParkinLot(int n)
{
  size = n;
  data.reserve(n);
}

void ParkinLot::push(Car info)
{
  data.push_back(info);
}

void ParkinLot::query(std::string &type, std::string &target)
{
  if (type == "brand")
  {
      for (const Car &item : data)
      {
          if (item.brand == target)
          {
              std::cout << item;
          }
      }
  }
  else
  {
      for (const Car &item : data)
      {
          if (item.color == target)
          {
              std::cout << item;
          }
      }
  }
}

// -----------------------------------------------------
// 入口函式

int main()
{
  int n, m;
  std::string brand, color, type, target;
  while (std::cin >> n >> m)
  {
      ParkinLot obj(n);
      while (n--)
      {
          std::cin >> brand >> color;
          obj.push(Car({brand, color}));
      }
      while (m--)
      {
          std::cin >> type >> target;
          obj.query(type, target);
      }
      std::cout << '\n';
  }
}

 

c++ 怎麼那麼多括號,想念 python 了嗚嗚......