日期:2014-05-20  浏览次数:20663 次

一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。在线等
一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第30位数是多少, 用递归算法实现。
package ff;

public class MainClass {
private static int a;

public int show(int i){

// Console.WriteLine(show(30));
if(i<=0){
a=0;
}
if(i==1&&i<=2){
a=1;

}
else{
a=show(i-1)+show(i-2);
}
return a;

}


public static void main(String[] args) {
MainClass aa=new MainClass();
aa.show(3);
System.out.println(a);


}

}

我写成这样了,现在就差怎样控制把30位输出?高手帮下。

------解决方案--------------------
/**
 * @(#)Output.java
 *
 *
 * @author 
 * @version 1.00 2007/10/5
 */


public class Output {
public static void main(String[] args) {
int a = show(30);
System.out.println(a);
}
public static int show(int n) {
int i = 1;
if(n < 0) {
System.out.println("请输入一个正数!");
}
else if(n == 1 || n == 2) {
i = 1;
}
else {
i = show(n-1) + show (n-2);
}
return i;
}

}
------解决方案--------------------
楼上的已经是非常完美了..我再打一遍..增加印象..
/**
*copywrite with you 
*
*/


public class OutPut{
public static void main(String args[]){
int a=30;
System.out.println(a);
}
public static int show(int n){
int i=1;
if(n<0){
System.out.pritnln("please input a ture number");
}
else if(n==1||n==2){
i=1;
}
else{
i=show(n-1)+show(i-2);
}
return i;
}

}