首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

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

一列数的规则如下: 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;
}

}

热点排行