递归函数转非递归:int f(int n) 和 int g(int n) 的优化
int f(int n) { int result; int f1 = 1; int f2 = 1; if (n <= 1) return 1; for (int i = 2; i <= n; i++) { result = f1 + g(f2); f1 = f2; f2 = result; } return result; }
int g(int n) { int result; int g1 = 1; int g2 = 1; if (n <= 1) return 1; for (int i = 2; i <= n; i++) { result = f(g2) + g(g1); g1 = g2; g2 = result; } return result; }
原文地址: https://www.cveoy.top/t/topic/qCt4 著作权归作者所有。请勿转载和采集!