c++輸入斐波那契數列示例分享。本站提示廣大學習愛好者:(c++輸入斐波那契數列示例分享)文章只能為提供參考,不一定能成為您想要的結果。以下是c++輸入斐波那契數列示例分享正文
CPS:http://en.wikipedia.org/wiki/Continuation-passing_style
示例代碼應用迭代 + 尾遞歸。
#include <stdio.h>
typedef void (*END_OF_END)(unsigned long);
void fibonacci(int, unsigned long, unsigned long, void(*)(unsigned long));
void
notify(unsigned long res) {
printf("Ultimate result: %lu\n" res);
exit(0);
}
/* Note: Fibonacci number might overflow! */
void
fibonacci(int n, unsigned long v1, unsigned long v2, void(*notify)(unsigned long)) {
if (n <= 0) notify(v2);
fibonacci(n - 1, v2, v1 + v2, notify);
}
int
main(void) {
fibonacci(100 - 2, 1, 1, notify);
return 0;
}