典型的DP題目,把需要遞迴計算的答案用陣列存起來即可。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
using namespace std; | |
int fn[101]={0}; | |
int f91 (int N){ | |
if (N > 100) return N-10; | |
if (fn[N] != 0) return fn[N]; | |
fn[N] = f91(f91(N+11)); | |
return fn[N]; | |
} | |
int main() | |
{ | |
int n; | |
while (scanf("%d",&n)){ | |
if (!n) break; | |
printf("f91(%d) = %d\n",n,f91(n)); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言