永发信息网

请编写一个Java程序,能够求出0-100之间的斐波那契数,并且将结果在控制台有序输出。用java语言

答案:4  悬赏:60  手机版
解决时间 2021-01-17 23:12
  • 提问者网友:川水往事
  • 2021-01-17 10:29
请编写一个Java程序,能够求出0-100之间的斐波那契数,并且将结果在控制台有序输出。用java语言
最佳答案
  • 五星知识达人网友:狂恋
  • 2021-01-17 12:04
public class fibonacci {
public static void main(String agrs []){
int n0=1,n1=1,n2;
System.out.print (n0+" "+n1+" ");
for(int i=0;i<=8; i++)
{n2=n1+n0;
System.out.print(n2+" ");
n0=n1;
n1=n2;}
System.out.println();}}
全部回答
  • 1楼网友:掌灯师
  • 2021-01-17 14:44
//递归,超级耗时: n=35的时候用了65s
//后面加一位,时间成倍增加
import java.math.*;
public class E08 {
public static void main(String[] a) {
BigInteger n = new BigInteger("38");
long t1 = System.currentTimeMillis(), t2;
System.out.println(fib(n) + "");
t2 = System.currentTimeMillis();
System.out.println(((t2 - t1) / 1000) + "s");
return;
}
static BigInteger fib(BigInteger b) {
BigInteger b1 = new BigInteger("1");
BigInteger b2 = new BigInteger("2");
BigInteger b3 = new BigInteger("-1");
BigInteger b4 = new BigInteger("-2");
if (b.equals("1") || b.equals("2"))
return b1;
else if (b.max(b2).equals(b))
return (fib(b.add(b3)).add(fib(b.add(b4))));
// return fib(n-1)+fib(n-2);
// 不添加这一语句会导致上面条件不满足是无法返回一个值
else
return b1;
}
}//for 循环,省时省力
//n=100时,用时15ms
import java.math.*;
public class E08_1 {
public static void main(String[] args) {
BigInteger b1 = new BigInteger("1");
BigInteger b2 = new BigInteger("1");
BigInteger br = new BigInteger("0");
long t1 = System.currentTimeMillis(), t2;
for (int i = 3; i <= 100; i++) {
br = b1.add(b2);
System.out.println("第" + i + "项为:   " + br);
b1 = b2;
b2 = br;
}
t2 = System.currentTimeMillis();
System.out.println("程序时间:" + (t2 - t1) + "ms");
}
}
  • 2楼网友:等灯
  • 2021-01-17 14:11
public class Test {
public static void main(String[] args) {
int i = 1;
while (test(i) <= 100) {
System.out.println(test(i));
i++;
}
}
public static int test(int i) {
if (i == 1 || i == 2) {
return 1;
} else if (i >= 3) {
return test(i - 1) + test(i - 2);
} else
return 0;
}
}
  • 3楼网友:举杯邀酒敬孤独
  • 2021-01-17 13:35
*************************************************************
介绍两种方法:递归算法FibonacciR.java和非递归算法FibonacciNonR.java, 代码分别如下:
*************************************************************
// 递归算法
public class FibonacciR {
public static void main(String[] args) {
int i = 1;
while (calFib(i) <= 100) {
System.out.println(calFib(i));
i++;
}
}
public static Integer calFib(int n) {
if (n == 1 || n == 2) {
return 1;
} else if (n >= 3) {
return calFib(n - 1) + calFib(n - 2);
}
return 0;
}
}
*************************************************************
// 非递归算法
public class FibonacciNonR {
public static void main(String[] args) {
int i = 1;
while (calFib(i) <= 100) {
System.out.println(calFib(i));
i++;
}
}
public static Integer calFib(int n) {
Integer n1 = 1;
Integer n2 = 1;
Integer s = 0;
if (n == 1 || n == 2) {
return n1;
}
for (int i = 3; i <= n; i++) {
s = n1 + n2;
n1 = n2;
n2 = s;
}
return s;
}
}
*************************************************************
运行结果均如下:
1
1
2
3
5
8
13
21
34
55
89
*************************************************************
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯