Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要为这个递归函数编写一个迭代函数。
int funcRec(int n){ if(n>1) { return 2*funcRec(n - 1) + 3*funcRec(n - 2); }else{ return n; } }
试试这个
int funcIter(int n) { int value0 = 0; int value1 = 1; int res = n; for (int i = 2; i <= n; i++) { res = 2 * value1 + 3 * value0; value0 = value1; value1 = res; } return res; }