구매한 물건의 가격과 개수로 계산한 총 금액이 영수증에 적힌 총 금액과 일치하는지 확인하는 함수 구현하기
준원이는 저번 주에 살면서 처음으로 코스트코를 가 봤다. 정말 멋졌다. 그런데, 몇 개 담지도 않았는데 수상하게 높은 금액이 나오는 것이다! 준원이는 영수증을 보면서 정확하게 계산된 것이 맞는지 확인해보려 한다.
영수증에 적힌,
X
가 주어진다.
𝑁
이 주어진다.
𝑁
개의 줄에는 각 물건의 가격 a
와 개수 b
가 공백을 사이에 두고 주어진다.
Yes
를 출력한다. 일치하지 않는다면 No
를 출력한다.
# input 1
260000
4
20000 5
30000 2
10000 6
5000 8
# output 1
Yes # 260000 = 260000
# input 2
250000
4
20000 5
30000 2
10000 6
5000 8
# output 2
No # 250000 != 260000
calculated_total
)을 구한다.
X
와 합을 비교해서, 일치하면 Yes
, 다르면 No
를 출력한다.
import sys
input = sys.stdin.read
data = input().split()
X = int(data[0])
N = int(data[1])
calculated_total = 0
index = 2
for _ in range(N):
a,b = map(int, data[index:index+2])
calculated_total += a * b
index +=2
if calculated_total == X:
print("Yes")
else:
print("No")
입력 값을 받는 방식에 따라 아래와 같이 구현할 수도 있다.
X = int(input.strip())
N = int(input.strip())
calculated_total = 0
for _ in range(N):
a,b = map(int, input().strip().split())
calculated_total += a * b
if calculated_total == X:
print("Yes")
else:
print("No")