package com.huawei.GC;
import java.util.Scanner;
/**
* 斐波那契数列
*
* @author Lpf.
* @version 创建时间:2019年5月10日 下午1:35:58
* @fib {1,1,2,3,5,8,13,21,24,45,69,114,183}
* @位数 {1,2,3,4,5,6,7, 8, 9, 10, 11, 12, 13}
*/
public class Fib {
//第一种方式
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = 0;
int y = 1;
System.out.print("请输入您要求的斐波那契数列的位数:");
int n = sc.nextInt();
for (int i = 1; i <= n - 1; i++) {
y = y + x;
x = y - x;
}
System.out.print(y);
}
//第二种方式
// public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// System.out.print("请输入您要求的斐波那契数列的位数:");
// int n = sc.nextInt();
// int x = 0;
// int y = 1;
// while (0 < --n) {
// y = y + x;
// x = y - x;
// }
// System.out.println(y);
// }
}
斐波那契数列
最新推荐文章于 2024-04-25 01:14:51 发布