Description
Recurrence relations are where a function is defined in terms of itself and maybe other functions as well. in this problem, we have two functions of interest:
F (N) = F (N - 1) + G (N - 1) ; F (1) = 1
G (N) = G (N - 1) + G (N - 3) ; G (1) = 1, G (2) = 0, G (3) = 1
For a given value of N, compute F (N).
Input
Each line of input will have exactly one integer, 57 > = N > 0.
Output
For each line of input, output F(N).
Sample Input
1 4 57
Sample Output
1 3 2035586497
Source:
#include<iostream>
using namespace std;

unsigned F[60]=...{0,1};
unsigned G[60]=...{0,1,0,1};
void list()
...{
int i;
for(i=4;i<=57;i++)
G[i]=G[i-1]+G[i-3];
for(i=2;i<=57;i++)
F[i]=F[i-1]+G[i-1];
}
int main()
...{
list();
int n;
while(cin>>n)
...{
cout<<F[n]<<endl;
}
return 0;
}
KEY: 不能使用递归的方法,会TLE的,所以采用数组来算,后面的只要出数组拿值;
本文介绍了一种通过数组而非递归方式计算两个特定递推关系中F(N)的方法,适用于N值范围为1到57的情况。给出了完整的C++实现代码,并提供了计算示例。

451

被折叠的 条评论
为什么被折叠?



