백준 1904 c++

magicdrill·2024년 3월 1일
0

백준 문제풀이

목록 보기
73/655

백준 1904 c++

재귀함수와 반복문을 통한 피보나치 수열을 구현했다.

#include <iostream>
/*
* N = 1일때 1개
* N = 2일때 2개
* N = 3일때 3개
* N = 4일때 5개
* N = 5일때 8개
* N = 6일때 13개 -> 피보나치수열 증가
*/

using namespace std;

int input(int lower, int upper)
{
	//cout << "input()" << endl;
	int A;

	while (1)
	{
		cin >> A;
		if (A >= lower && A <= upper)
		{
			break;
		}
		else
		{
			;
		}
	}

	return A;
}

//long long fibonacci(long long n)
//{
//	if (n == 1 || n == 0)
//	{
//		return 1;
//	}
//	else
//	{
//		return fibonacci(n - 1) + fibonacci(n - 2);
//	}
//}

long long fibonacci(long long N)
{
	int i;
	long long total = 0;
	long long temp1= 0, temp2 = 1;

	for (i = 0; i < N; i++)
	{
		total = (temp1 + temp2)%15746;
		temp1 = temp2;
		temp2 = total;
	}

	return total;
}

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	long long N;
	long long total;

	N = input(1, 1000000);
	total = fibonacci(N);

	cout << total << "\n";

	return 0;
}

0개의 댓글