๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 1003๋ฒˆ - ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜

by dkswnkk 2021. 10. 16.

 

 

1003๋ฒˆ: ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜

๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค 0์ด ์ถœ๋ ฅ๋˜๋Š” ํšŸ์ˆ˜์™€ 1์ด ์ถœ๋ ฅ๋˜๋Š” ํšŸ์ˆ˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

//  Copyright © 2021 ์•ˆ์ฃผํ˜•. All rights reserved.
//
//  https://www.acmicpc.net/problem/1003
//  BOJ1003 ํ”ผ๋ณด๋‚˜์น˜ ํ•จ์ˆ˜
#include <iostream>
#include <vector>

using namespace std;
int dp[41][2] = { {1,0},{0,1} };    // dp[0][0]=1; dp[0][0]=0; dp[1][0]=0; dp[1][1]=1;
                                    //num:0 ์ผ๋•Œ 0์ด ์ถœ๋ ฅ๋˜๋Š”๊ฒฝ์šฐ 1๊ฐœ 1์ด ์ถœ๋ ฅ๋˜๋Š” ๊ฒฝ์šฐ0๊ฐœ
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);


    int T; cin >> T;
    while (T--) {
        int N; cin >> N;
        for (int i = 2; i <=N; i++) {
            dp[i][0] = dp[i - 1][0] + dp[i - 2][0];    //0์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฐฏ์ˆ˜ ์ €์žฅ
            dp[i][1] = dp[i - 1][1] + dp[i - 2][1];    //1์ด ์‚ฌ์šฉ๋˜๋Š”  ๊ฐฏ์ˆ˜ ์ €์žฅ
        }
        cout << dp[N][0] << " " << dp[N][1] << "\n";
    }
}

๋Œ“๊ธ€