第一个记忆化搜索,理解得差不多啦。贴下代码咯~
CODE:
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include < string.h>
4 using namespace std;
5
6 int FIB[ 1001];
7
8 int fib( int n)
9 {
10 int i;
11 if(n > 2) return FIB[n] == - 1 ? FIB[n]=fib(n- 1)+fib(n- 2):FIB[n];
12 else return 1;
13
14 }
15
16
17 int main()
18 {
19 int n;
20 memset(FIB, - 1, sizeof(FIB));
21 FIB[ 0] = FIB[ 1] = 1;
22 while(~scanf( " %d ", &n))
23 {
24 printf( " %d\n ", fib(n));
25 }
26 return 0;
2 #include <stdlib.h>
3 #include < string.h>
4 using namespace std;
5
6 int FIB[ 1001];
7
8 int fib( int n)
9 {
10 int i;
11 if(n > 2) return FIB[n] == - 1 ? FIB[n]=fib(n- 1)+fib(n- 2):FIB[n];
12 else return 1;
13
14 }
15
16
17 int main()
18 {
19 int n;
20 memset(FIB, - 1, sizeof(FIB));
21 FIB[ 0] = FIB[ 1] = 1;
22 while(~scanf( " %d ", &n))
23 {
24 printf( " %d\n ", fib(n));
25 }
26 return 0;
27 }