-
[백준 17293번] 맥주 99병 - Java //Wello Horld//Algorithm/BOJ(Baekjoon Online Judge) 2019. 6. 29. 17:34
BOJ 천하제일 코딩대회예선 C번 문제인 맥주 99병을 풀어봅시다
아니, 거 문제가 너무 긴거 아니오??
일단 문제를 쉽게 해석해보면
처음에 자연수 N이 입력으로 주어지고, 처음에 벽에 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.K = 0 이 될때 까지 K를 빼주면서 위에 문장을 출력해주면 된다
예외상황으로, K = 2 일 때,
2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.
K = 1 일 때는,
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.
K = 0 일 때는,
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 = 1 이고 K = 0 일때, "N bottles" 가 아니라 "N bottle"을 출력해줘야 되네;;
헤헤헤 그래서 전체 코드는
import java.io.*; import java.util.*; public class sample { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); int N = sc.nextInt(); for (int K = N; K >= 0; K--) { if (K == 0) { if(N == 1){ bw.write("No more bottles of beer on the wall, no more bottles of beer.\n" + "Go to the store and buy some more, " + N + " bottle of beer on the wall.\n"); } else { bw.write("No more bottles of beer on the wall, no more bottles of beer.\n" + "Go to the store and buy some more, " + N + " bottles of beer on the wall.\n"); } } else if (K == 1) { bw.write("1 bottle of beer on the wall, 1 bottle of beer.\n" + "Take one down and pass it around, no more bottles of beer on the wall.\n"); } else if (K == 2) { bw.write("2 bottles of beer on the wall, 2 bottles of beer.\n" + "Take one down and pass it around, 1 bottle of beer on the wall.\n"); } else { bw.write(K + " bottles of beer on the wall, " + K + " bottles of beer.\n" + "Take one down and pass it around, " + (K - 1) + " bottles of beer on the wall.\n"); } bw.write("\n"); } bw.flush(); sc.close(); bw.close(); } }
이렇게 하드하게 풀었다
코드 길이 줄이기는 귀찮아서 패스할게요
즐거운 주말코딩 되세요~
'Algorithm > BOJ(Baekjoon Online Judge)' 카테고리의 다른 글
[백준 2798번] 블랙잭 - Java //Wello Horld// (0) 2019.07.04 [백준 3052번] 나머지 - Java //Wello Horld// (0) 2019.07.04 [백준 4949번] 균형잡힌 세상 - Java //Wello Horld// (0) 2019.06.29 [백준 17295번] 엔드게임 스포일러 - Java //Wello Horld// (0) 2019.06.29 [백준 17294번] 귀여운 수~ε٩(๑> ₃ <)۶з - Java //Wello Horld// (0) 2019.06.27