프로그래밍/백준

[알고리즘] 백준 17293 파이썬

매 석 2022. 10. 17. 12:20
반응형

 

17293번: 맥주 99병

99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall. 98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall. (중략) 1 bottle of

www.acmicpc.net

 

문제

99 Bottles of Beer라는 노래의 가사는 Hello World처럼 프로그래밍 연습 예제로 자주 쓰인다. 우리의 목표는 N Bottles of Beer를 부르는 것이다. 고등학생이 맥주를 마시는 것은 세계로 미래로 꿈을 펼치는 선린인의 준법정신에 맞지 않지만 정말로 맥주를 마시는 게 아니라 노래만 부르면 되므로 상관은 없다.

N Bottles of Beer의 가사는 다음 과정을 통해 만들어진다. 현재 벽에 K병의 맥주가 있다고 하자. 맨 처음에는 K = N이다. 이때 맥주 한 병을 따면서 다음을 출력한다.

K bottles of beer on the wall, K bottles of beer. Take one down and pass it around, K-1 bottles of beer on the wall.

단, 맥주가 한 병만 있음을 표현하려면 1 bottles가 아니라 1 bottle이라고 해야 한다. 또한 맥주가 한 병도 없음을 표현하려면 0 bottles가 아니라 no more bottles라고 해야 한다.

맥주가 아직 남아있으면 위 과정을 반복하고, 더 이상 남아있지 않으면 다음을 출력하고 종료한다. 마찬가지로 맥주를 한 병만 사오는 경우 1 bottles가 아니라 1 bottle이라고 해야 한다.

No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, N bottles of beer on the wall.

문제풀이

N = int(input())
a=""
b=""
for i in range(N+1):
    if i==N-1:
        a="1 bottle"
        b="no more bottles"
        print(a+" of beer on the wall, "+a+" of beer.")
        print("Take one down and pass it around, "+b+" of beer on the wall.")
        print()
    elif i==N-2:
        a="2 bottles"
        b="1 bottle"
        print(a+" of beer on the wall, "+a+" of beer.")
        print("Take one down and pass it around, "+b+" of beer on the wall.")
        print()
    elif i ==N:
        a="No more bottles"
        if(N==1):
            b="1 bottle"
        else:
            b=str(N)+" bottles"
        c="no more bottles"
        print(a+" of beer on the wall, "+c+" of beer.")
        print("Go to the store and buy some more, "+b+" of beer on the wall.")
    else:
        a=str(N-i)+" bottles"
        b=str(N-i-1)+" bottles"
        print(a+" of beer on the wall, "+a+" of beer.")
        print("Take one down and pass it around, "+b+" of beer on the wall.")
        print()