Coding_Test_C++

BaekJoon 2193번: 이친수

펄크럼 2021. 4. 21. 09:02

풀이법

규칙을 구하기 위해 각 항의 개수를 트리 형태로 만들어보았다.

1 1 2 3 5 8 과 같은 규칙을 발견할 수 있었으며, 이는 곧 피보나치 수열에 해당한다.

배열의 크기가 90이상일 수 있으니 꼭 long long형으로 선언해야 한다.

#include <iostream>
#include<vector>
#include <algorithm>
using namespace std;

int n;
long long arr[91];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	arr[1] = 1;
	arr[2] = 1;
	for (int i = 3; i <= n; i++)
	{
		arr[i] = arr[i - 1] + arr[i - 2];
	}
	cout << arr[n]<<"\n";
}

github.com/pearlcrum/CodingTest/tree/main

 

pearlcrum/CodingTest

ForPracticingCodingTest. Contribute to pearlcrum/CodingTest development by creating an account on GitHub.

github.com