“`” 参考回答:

递归:

<pre><code>int Fib(int n) {if (1 == n || 2 == n)return n;elsereturn Fib(n – 1) + Fib(n – 2);}}</code></pre>

非递归:

<pre><code>public static int calc3(int n) {if (1 == n || 2 == n)return n;int s1 = 1, s2 = 2, s3 = 1;for (int i = 3; i <= n; i++) {s3 = s1 + s2;s1 = s2;s2 = s3;}return s3;}</code></pre>

 

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.