永发信息网

用java代码输出斐波那数列前100项?

答案:3  悬赏:10  手机版
解决时间 2021-01-25 06:22
  • 提问者网友:龅牙恐龙妹
  • 2021-01-25 03:23
用java代码输出斐波那数列前100项?
最佳答案
  • 五星知识达人网友:woshuo
  • 2021-01-25 04:57
import java.util.ArrayList;
import java.util.List;

public class Test {

public static void main(String[] args) {

List list = new ArrayList();

int i = 1;

while(i <= 100){
if(i <= 2){
list.add(new Long(1));
}else{
long value = list.get(i-2) + list.get(i - 3);
list.add(new Long(value));
}

i++;
}

int count = 1;
for(i = 0; i < list.size(); i++){
if(count == 5){
System.out.println();
count = 0;
}

System.out.print(list.get(i).longValue() + " ");
count++;
}

}

}
全部回答
  • 1楼网友:鱼芗
  • 2021-01-25 06:37
long x=1,y=1; System.out.println(x+" "); for(int i=1;i<=100;i++){ System.out.println(y+" "); y=x+y; x=y-x; }
  • 2楼网友:孤独入客枕
  • 2021-01-25 05:45
斐波纳契数列要用递归学,如 package practice; public class function { public static void main(string[] args) { system.out.println(f(4)); } public static int f(int n){ if(n==1) return 1; if(n==2) return 2; return f(n-1)+f(n-2); } } 递归方法一定要有出口,其中n=1和n=2就是出口。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯