๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 2748๋ฒˆ : ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ 2

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 9. 18.

 

 

Algorithm

-  ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜ 2 -

 

 


 

๋ฌธ์ œ

ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 0๊ณผ 1๋กœ ์‹œ์ž‘ํ•œ๋‹ค. 0๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 0์ด๊ณ , 1๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋Š” 1์ด๋‹ค. ๊ทธ ๋‹ค์Œ 2๋ฒˆ์งธ ๋ถ€ํ„ฐ๋Š” ๋ฐ”๋กœ ์•ž ๋‘ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์˜ ํ•ฉ์ด ๋œ๋‹ค.

์ด๋ฅผ ์‹์œผ๋กœ ์จ๋ณด๋ฉด Fn = Fn-1 + Fn-2 (n ≥ 2)๊ฐ€ ๋œ๋‹ค.

n=17์ผ๋•Œ ๊นŒ์ง€ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ์จ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597

n์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, n๋ฒˆ์งธ ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

 

๋ฌธ์ œ ํ’€๊ธฐ

const readline = require("readline");
const rl = readline.createInterface({
	input: process.stdin,
	output: process.stdout,
});
let input;
rl.on("line", function (line) {
	input = +line.toString();
}).on("close", function () {
	let fibo = [0, 1];
	while (fibo.length <= input) {
		fibo.push(BigInt(fibo[fibo.length - 1]) + BigInt(fibo[fibo.length - 2]));
	}
	console.log(String(fibo[input]));
	process.exit();
});

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€