JAVA写一个程序打印斐波拉契 (Fibonacci) 数列,数列的长度可以由输入指定。 斐波拉契 (Fibonacci) 数列是的前两项分别是1和1,后续各项分别是前面两项的和。 1 1 2 3 5 8 13 21 34………… 至少用2种方式实现。
时间: 2023-09-16 09:05:36 浏览: 94
### 回答1:
第一种方式:使用递归实现斐波那契数列
```
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入斐波那契数列的长度:");
int n = input.nextInt();
for (int i = 1; i <= n; i++) {
System.out.print(fibonacci(i) + " ");
}
}
public static int fibonacci(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
```
第二种方式:使用循环实现斐波那契数列
```
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入斐波那契数列的长度:");
int n = input.nextInt();
int[] fib = new int[n];
fib[0] = 1;
fib[1] = 1;
for (int i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
for (int i = 0; i < n; i++) {
System.out.print(fib[i] + " ");
}
}
}
```
### 回答2:
第一种方式:
```java
import java.util.Scanner;
public class FibonacciExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入斐波拉契数列的长度:");
int n = scanner.nextInt();
// 初始化前两个数
int first = 1;
int second = 1;
System.out.print("斐波拉契数列前 " + n + " 项为:");
System.out.print(first + " " + second + " ");
for (int i = 3; i <= n; i++) {
int curr = first + second;
System.out.print(curr + " ");
// 更新前两个数
first = second;
second = curr;
}
}
}
```
第二种方式:
```java
import java.util.Scanner;
public class FibonacciExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入斐波拉契数列的长度:");
int n = scanner.nextInt();
System.out.print("斐波拉契数列前 " + n + " 项为:");
for (int i = 1; i <= n; i++) {
System.out.print(fibonacci(i) + " ");
}
}
public static int fibonacci(int n) {
if (n == 1 || n == 2) {
return 1;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
}
```
以上两种方式都可以打印出指定长度的斐波拉契数列。第一种方式使用循环来逐个计算并打印数列中的每一项,而第二种方式使用递归来计算数列中的每一项并打印。
### 回答3:
方式一:
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要打印的斐波拉契数列的长度:");
int length = scanner.nextInt();
int[] fibonacci = new int[length];
fibonacci[0] = 1;
fibonacci[1] = 1;
for (int i = 2; i < length; i++) {
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
}
for (int i = 0; i < length; i++) {
System.out.print(fibonacci[i] + " ");
}
}
}
方式二:
public class Fibonacci {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要打印的斐波拉契数列的长度:");
int length = scanner.nextInt();
int firstNum = 1;
int secondNum = 1;
System.out.print(firstNum + " " + secondNum + " ");
for (int i = 3; i <= length; i++) {
int currentNum = firstNum + secondNum;
System.out.print(currentNum + " ");
firstNum = secondNum;
secondNum = currentNum;
}
}
}
以上是两种方式实现打印斐波拉契数列的程序,通过输入指定数列的长度,程序将计算并打印出相应长度的斐波拉契数列。方式一使用数组存储数列中的数字,并通过循环计算每一项的值;方式二则直接打印出每一项的值,通过变量保存前两项的值用于计算后续的项。
阅读全文