백준/브론즈3

[Python] 백준 3512번 - Flat

두부마라탕 2025. 4. 15. 17:29

https://www.acmicpc.net/problem/3512


문제

You are one of the developers of software for a real estate agency. One of the functions you are to implement is calculating different kinds of statistics for flats the agency is selling. Each flat consists of different types of rooms: bedroom, bathroom, kitchen, balcony and others.

The cost of the flat is equal to the product of reduced total area and the cost of one square metre. Reduced total area is the total area of all rooms except for balconies plus one half of balconies total area.

You will be given some information about the area of each room in the flat and the cost of one square metre. You are to calculate the following values for the flat:

  • the total area of all rooms;
  • the total area of all bedrooms;
  • the cost of the flat.

내가 부동산 중개 소웨를 만들어야 하는데 방들의 총합 면적과 침실의 총합 면적, 그리고 아파트의 비용을 알려줘야 한다. 그런데 아파트 비용을 책정할 때 할인이 들어가게 된다. 할인되어 나온 아파트 가격은 발코니를 제외한 전체 방 면적에 발코니 면적 절반을 합한 크기에 주어진 1제곱미터 당 가격을 곱해주는 것이다.

 

코드

num, price = map(int, input().split())
total = bedroom = discount = 0

for i in range(num):
    area, room = map(str, input().split())
    area = int(area)
    total += area
    if room == "balcony":
        area /= 2
    discount += area
    if room == "bedroom":
        bedroom += area

print(total)
print(bedroom)
print(f"{discount * price:.6f}".rstrip('0').rstrip('.'))

 

풀이

처음에 주어진 입출력 예시만 보고 bedroom에 +=를 해줘야 하는데 = 이것만 해줘서 계속 틀렸었다... 침실이 여러 개인 경우도 있으니 꼭! +를 넣도록 하자...

 

1. 일단 방의 수와 1 제곱미터당 가격을 받아준다.

2. 방의 수만큼 반복을 해주고 각 방의 면적과 명칭을 받는다.

3. str로 받았기 때문에 면적을 정수로 바꿔준 후 전체 면적에 더해준다.

4. 발코니의 경우 가격을 책정할 때 면적의 절반만 들어가기에 2를 나눠준 후 할인 면적에 넣어준다

5. 그리고 침실의 경우 침실 면적을 따로 출력해야 하기에 bedroom에 더해준다.

6. 이제 전체 면적과 침실 면적, 그리고 할인된 아파트 가격을 출력해 준다.

    6-1. 출력 조건이 소수점 최대 6자리라 f-string형식을 사용하였고, 불필요한 0이나 정수일 경우 .0을 빼줘야 해서 rstrip을 사용하여 결괏값을 깔끔하게 만들어주었다.