其他分享
首页 > 其他分享> > ZROI2017 做题笔记

ZROI2017 做题笔记

作者:互联网

B2064 斐波那契数列

递归函数的写法应该是:

1 int f(int n)
2 {
3     if (n <= 2) return 1;
4     return f(n - 1) + f(n - 2);
5 }

改成非递归的手工栈需要储存函数参数、时间戳和答案,比较恶心但也的确是 C++ 递归函数内部代码的形式。

 1 const int N = 1e6 + 10;
 2 int s[N][3];
 3 
 4 int f(int n)
 5 {
 6     int head = 1; s[1][0] = n, s[1][1] = s[1][2] = 0;
 7     while (head)
 8     {
 9         if (!s[head][1])
10             if (s[head][0] <= 2) s[head--][2] = 1;
11             else s[head][1] = 1, ++head, s[head][0] = s[head - 1][0] - 1, s[head][1] = s[head][2] = 0;
12         else if (s[head][1] & 1)
13         {
14             s[head][1] = 2, s[head][2] += s[head + 1][2];
15             ++head, s[head][0] = s[head - 1][0] - 2, s[head][1] = s[head][2] = 0;
16         }
17         else s[head][2] += s[head + 1][2], --head;
18     }
19     return s[1][2];
20 }

To be continued...

标签:10,head,递归函数,int,笔记,函数参数,1e6,ZROI2017
来源: https://www.cnblogs.com/jhqqwq/p/15974636.html