• 문제 링크
15841번: Virus Outbreak
For each input value, the output contains a line in the format: Hour X: Y cow(s) affected, where X is the hour, and Y is the total affected cows that need to be euthanized based on the hour given by X.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
BigInteger[] cache = new BigInteger[491];
cache[0] = BigInteger.ZERO;
cache[1] = BigInteger.ONE;
for (int i = 2; i < cache.length; i++) cache[i] = cache[i - 1].add(cache[i - 2]);
int n;
while ((n = read()) != -1)
sb.append("Hour ").append(n).append(": ").append(cache[n]).append(" cow(s) affected\n");
bw.write(sb.toString());
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
boolean flag = n == 13;
if (flag) n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return flag ? ~n + 1 : n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 1977 완전제곱수 - Brute Force / Java (0) | 2024.01.23 |
---|---|
[백준] 6193 Hungry Cows - Dynamic Programming / Java (0) | 2024.01.22 |
[백준] 6221 The Bale Tower - Dynamic Programming / Java (0) | 2024.01.20 |
[백준] 10752 Cow Hopscotch - Dynamic Programming / Java (0) | 2024.01.19 |
[백준] 11867 박스 나누기 게임 - Dynamic Programming / Java (0) | 2024.01.18 |
댓글