문제
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.
https://www.acmicpc.net/problem/3512
입력
The first line of the input file contains two integer numbers n (1 ≤ n ≤ 10) and c (1 ≤ c ≤ 100 000) — number of rooms in the flat and the cost of one square metre, respectively.
Each of the following n lines contains an integer number ai (1 ≤ ai ≤ 100) and a word ti — the area of i-th room and its type, respectively. Word ti is one of the following: “bedroom”, “bathroom”, “kitchen”, “balcony”, “other”.
출력
The first line of the output file should contain one integer number — the total area of all rooms of the flat.
The second line of the output file should contain one integer number — the total area of bedrooms of the flat.
The third line of the output file should contain one real number — the cost of the flat with precision not worse than 10−6.
예제
# input
6 75000
8 other
3 bathroom
2 bathroom
10 kitchen
16 bedroom
7 balcony
# output
46
16
3187500
# input
2 75123
10 kitchen
15 balcony
# output
25
0
1314652.5
문제 풀이
# 정답 코드
import sys
input = sys.stdin.readline
n, c = map(int, input().split())
D = dict.fromkeys(['bedroom', 'bathroom', 'balcony', 'kitchen', 'other'], 0)
for i in range(n):
num, room = input().split()
D[room] += int(num)
print(sum(D.values()))
print(D['bedroom'])
print((sum(D.values())-D['balcony']/2)*c)
# 오답 코드 (bedroom, balcony가 반드시 input된다는 보장이 없기에 key error 발생)
import sys
input = sys.stdin.readline
n, c = map(int, input().split())
D = dict()
for i in range(n):
num, room = input().split()
if room not in D:
D[room] = int(num)
else:
D[room] += int(num)
print(sum(D.values()))
print(D['bedroom'])
print((sum(D.values())-D['balcony']/2)*c)
파이썬을 독학하시는 분들에게 도움이 되길 바라며,
혹 더 좋은 방법이 있거나 오류가 있다면 편하게 말씀 부탁드립니다.