티스토리 뷰

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

 

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

 

/**
 *  피보나치 수열
 * 
 * dp[i]: i는 시간, dp[i]는 i시간에 죽여야할 소의 마리 수
 * 
 * 조건식
 * dp[i] = dp[i-1]+ dp[i-1]
 * 
 * 초기 값
 * dp[1] = 1;
 * dp[2] = 1;
 * 
 * 주의 사항
 * BigInt 
 *
 */

const input = require('fs').readFileSync('./dev/stdin').toString().trim().split('\n').map(Number);
const max = input.reduce((r, v) => {
	r = Math.max(r, v);
	return r;
}, -1);

let dp = Array(max + 1).fill(BigInt(1));

for (let i = 3; i <= 490; i++) {
	dp[i] = dp[i - 1] + dp[i - 2];
}

const answer = [];
for (let i = 0; i < input.length; i++) {
	const h = input[i];
	if (h == -1) {
		console.log(answer.join('\n'));
		process.exit();
	}

	answer.push(`Hour ${h}: ${dp[h]} cow(s) affected`);
}
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함