c语言的cps实现求fibonacci数列示例
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;
}
相关文章
C++11新特性之随机数库(Random Number Library)详解
相对于C++11之前的随机数生成器来说,C++11的随机数生成器是复杂了很多,下面这篇文章主要给大家介绍了关于C++11新特性之随机数库(Random Number Library)的相关资料,需要的朋友可以参考下2022-06-06
最新评论