[Python]_거스름돈 계산기

hanseungjune·2022년 6월 9일
0

Python

목록 보기
3/38

풀이

def calculate_change(payment, cost):
    # 코드를 작성하세요.
    change = payment - cost
    
    fiveMill_count = (change - (change%50000))/50000
    print("{0}원 지폐: {1:.0f}장".format("50000",fiveMill_count))
    change -= 50000 * fiveMill_count
    
    oneMill_count = (change - (change%10000))/10000
    print("{0}원 지폐: {1:.0f}장".format("10000",oneMill_count))
    change -= 10000 * oneMill_count
    
    fiveThou_count = (change - (change%5000))/5000
    print("{0}원 지폐: {1:.0f}장".format("5000",fiveThou_count))
    change -= 5000 * fiveThou_count
    
    oneThou_count = (change - (change%1000))/1000
    print("{0}원 지폐: {1:.0f}장".format("1000",oneThou_count))
    change -= 1000 * oneThou_count
    
# 테스트
calculate_change(100000, 33000)
print()
calculate_change(500000, 378000)

저번에 풀어 봤던 문제인데, 확실하게 파이썬이 문장을 구성하기에는 편하고 깔끔하다는 느낌이 들었다. 그래서 알고리즘은 파이썬으로 해야겠다는 생각이 들었음

change를 잔돈으로 설정하였고, count 시리즈를 각각의 지폐의 수를 나타냄

잔돈에서 잔돈주고 나오는 나머지를 빼면 지폐를 몇장 줘야하는지 대략 계산이 된다. 그래서 가능함
그리고 출력 시에 나누기라서 소숫점이 나와서 해당 코드처럼 :.0f 로 표시했다.

profile
필요하다면 공부하는 개발자, 한승준

0개의 댓글