[백준] 9461번 파도반 수열 / C++
#문제
#풀이
#include <iostream>
using namespace std;
long long hypotenuses[101];
void triangle(int n)
{
if (n > 8)
{
for (int i = 9; i <= n; ++i)
{
hypotenuses[i] = hypotenuses[i - 1] + hypotenuses[i - 5];
}
}
cout << hypotenuses[n] << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t, n;
cin >> t;
hypotenuses[1] = 1;
hypotenuses[2] = 1;
hypotenuses[3] = 1;
hypotenuses[4] = 2;
hypotenuses[5] = 2;
hypotenuses[6] = 3;
hypotenuses[7] = 4;
hypotenuses[8] = 5;
while (t--)
{
cin >> n;
triangle(n);
}
return 0;
}
#정리
파도반 수열은 8개 이후부터 규칙성을 보이기 시작한다. hypotenuses[i] = hypotenuses[i - 1] + hypotenuses[i - 5] 파도반 수열은 숫자가 기하급수적으로 증가하기 때문에 int가 담을 수 있는 데이터의 양을 금방 초과한다. 데이터 타입을 long long으로 만들어 주면 쉽게 해결할 수 있다.
Enjoy Reading This Article?
Here are some more articles you might like to read next: