hades

[Baekjoon] 1003๋ฒˆ: ํ”ผ๋ณด๋‚˜์น˜ ํ•ฉ ๋ณธ๋ฌธ

๐Ÿ‘Š PS/Algorithm

[Baekjoon] 1003๋ฒˆ: ํ”ผ๋ณด๋‚˜์น˜ ํ•ฉ

hades1 2024. 7. 2. 19:48

๐Ÿฅ… ๋ฌธ์ œ

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

 

๐Ÿ” ์„ค๊ณ„

์žฌ๊ท€ ํ•จ์ˆ˜๋กœ ์„ค๊ณ„ํ•  ๊ฒฝ์šฐ, ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ด๋‹ค.

 

fibonacci(N)์„ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ, 0๊ณผ 1์˜ ํ˜ธ์ถœ ํšŸ์ˆ˜๋Š” fibonacci(N-1)๊ณผ fibonacci(N-2)์˜ 0๊ณผ 1์˜ ํ˜ธ์ถœ ํšŸ์ˆ˜์™€ ๊ด€๋ จ์ด ์žˆ์œผ๋ฏ€๋กœ, ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ด์šฉํ•˜์—ฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ‘Š ํ’€์ด

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int t, n;
vector<pair<int, int>> dp(41, {0, 0});

int main() {
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin >> t;
	dp[0].first = 1;
	dp[1].second = 1;
	for (int i=2; i<=40; i++){
		dp[i].first = dp[i-1].first + dp[i-2].first;
		dp[i].second = dp[i-1].second + dp[i-2].second;
	}
	for (int j=0; j<t; j++){
		cin >> n;
		cout << dp[n].first << " " << dp[n].second << "\n";
	}
	return 0;
}

 

๐Ÿ“ ๋ฉ”๋ชจ

A์˜ ๊ฐ’์„ ๊ตฌํ•˜๋Š”๋ฐ B์™€ C์˜ ๊ฐ’์„ ์ด์šฉํ•œ๋‹ค๋ฉด, ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.