๋ฌธ์
์ฝ๋
#include <iostream>
using namespace std;
int A[46],B[46];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
A[0]=1; B[0]=0;
A[1]=0; B[1]=1;
A[2]=1; B[2]=1;
for(int i=3; i<=n; i++){
A[i] = B[i-1];
B[i] = A[i-1] + B[i-1];
}
cout<<A[n]<< ' '<<B[n];
}
ํ์ด
๊ฐ๋จํ DP๋ฌธ์ ์์ต๋๋ค.
A[0] =1, B[0] = 0 (์ ์ผ ์ด๊ธฐ ์ํ)
A[1]=0, B[1] = 1 (ํ๋ฒ ๋๋ ์ ๋)
A[2]=1, B[2] = 1 (๋ ๋ฒ ๋๋ ์ ๋)
์ด๋ ๊ฒ ์ด๊ธฐ๊ฐ์ ์ค์ ํด์ฃผ๊ณ ์ดํด๋ณด๋ฉด A[n] = B[n-1], B[n] = A[n-1] + B[n-1] ์ ์ ํ์์ ๋์ถํด๋ผ ์ ์์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 21918๋ฒ - ์ ๊ตฌ (0) | 2022.03.01 |
---|---|
[๋ฐฑ์ค,c++] 14888๋ฒ - ์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ (0) | 2022.02.17 |
[๋ฐฑ์ค,c++] 10211๋ฒ - Maximum Subarray (0) | 2022.01.04 |
[๋ฐฑ์ค,c++] 15688๋ฒ - ์ ์ ๋ ฌํ๊ธฐ5 (0) | 2021.12.14 |
[๋ฐฑ์ค,c++] 15666๋ฒ - N๊ณผ M (12) (0) | 2021.12.04 |
[๋ฐฑ์ค,c++] 15665๋ฒ - N๊ณผ M (11) (0) | 2021.12.04 |
๋๊ธ