문제

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

 

25304번: 영수증

준원이는 저번 주에 살면서 처음으로 코스트코를 가 봤다. 정말 멋졌다. 그런데, 몇 개 담지도 않았는데 수상하게 높은 금액이 나오는 것이다! 준원이는 영수증을 보면서 정확하게 계산된 것

www.acmicpc.net

구매한 각 물건의 가격과 총합이 일치하는지 확인해야 한다.

 

풀이

야 우리집에서 이마트 털러 가도 26만원어치는 못사… 게임팩을 털어왔나 아무튼 그래요… 가랑비에 옷 젖는 줄 모른다는 말이 이럴 때 쓰는 말임…

아무튼 로직이 2단인데

  1. 다 더한다
  2. 비교한다

이게 다다.

import sys
total_price = int(sys.stdin.readline())
what_buy = int(sys.stdin.readline())
# 총합과 물건 종류 수
if_total = 0
for i in range(what_buy):
    price, amount = map(int, sys.stdin.readline().split())
    if_total += price * amount
# 더함
if if_total - total_price == 0:
    print("Yes")
else: 
    print("No")
# 끝

그래서 이게 다다. 엥? if에 왜 if_total – total_price == 0이 들어갔어요? 두개 똑같으면 뺐을때 0이잖음.

import sys
total_price = int(sys.stdin.readline())
what_buy = int(sys.stdin.readline())
# 총합과 물건 종류 수
if_total = 0
i = 1
while i <= what_buy:
    price, amount = map(int, sys.stdin.readline().split())
    if_total += price * amount
    i += 1
# 더함
if if_total - total_price == 0:
    print("Yes")
else: 
    print("No")
# 끝

While은 이거.

import sys
total_price = int(sys.stdin.readline())
what_buy = int(sys.stdin.readline())
# 총합과 물건 종류 수
if_total = 0
while True:
    try:
        price, amount = map(int, sys.stdin.readline().split())
        if_total += price * amount
    except:
        break
# 더함
if if_total - total_price == 0:
    print("Yes")
else: 
    print("No")
# 끝

While True는 트라이 익셉 주면 되는데, 이건 틀린다. 왜냐하면 입력할 개수를 지정해줬기 때문에 While True가 의미가 없기 때문.

'BOJ > [BOJ] Python' 카테고리의 다른 글

백준 1436번 풀이  (0) 2022.08.20
백준 1018번 풀이  (0) 2022.08.20
백준 3003번 풀이  (0) 2022.08.20
백준 7568번 풀이  (0) 2022.08.19
백준 2231번 풀이  (0) 2022.08.19

Profile

Lv. 34 라이츄

요즘 날씨 솔직히 에바참치김치꽁치갈치넙치삼치날치기름치준치학꽁치임..