2021-01-26

斐波那契序列算法:
package com.ncit.simulationsystem.chenxy;

import java.util.Scanner;

public class CXYFibonacciSequenceMainAndTest {
static int count=0;
public static void main(String[] args)
{
int t,n;
Scanner in = new Scanner(System.in);
System.out.println(“请输入正整数递归级数:”);
n=in.nextInt();
if(n equals 1)
{
int m=1;
System.out.println(“原始斐波那契数列:”);
System.out.println(m);
}
if(n equals 2)
{
int m=1;
int q=1;
System.out.println(“原始斐波那契数列:”);
System.out.print(m+" “+q+” “);
}
if(n>2)
{
int p=n+2;
double[] g = new double[p];
System.out.printf(“原始斐波那契数列:”);
g[0]=1;
System.out.println(g[0]+” “);
g[1]=1;
System.out.println(g[1]+” “);
g[2]=g[0]+g[1];
for(int i=2;i {
if(i==2)
{
System.out.println(g[i]+” “);
}
else
{
g[i]=g[i-1]+g[i-2];
System.out.println(g[i]+” ");
}
}
}
}
}

你可能感兴趣的:(算法)