算法分析与设计——要求根据给定的正整数n计算第n个斐波那契数。

本实验要求根据给定的正整数n计算第n个斐波那契数。
1、设计一个交互界面(例如菜单)供用户选择,如果可能,最好是一个图形化用户界面。
2、利用迭代算法寻找不超过编程环境能够支持的最大整数的斐波那契数是第几个斐波那契数。(Java: 231-1 for int, 263-1 for long)
3、根据第二步计算的最大的斐波那契数序号n,采用递归方式计算第n个斐波那契数,看其是否可以在1分钟内完成。
4、利用递归算法计算你的计算机能够在30秒内计算出的最大斐波那契数是第几个,利用迭代算法求解同样的问题。
5、利用公式F(n) = [n/sqrt(5)]快速计算第n个斐波那契数,找出出现误差时的最小n值。
6、利用矩阵相乘方法计算第n个斐波那契数。
对于相同的输入n值,比较上述四种方法的基本操作次数,以掌握对数、线性和指数增长率的极大差别。

题目一:

do{
            Scanner s=new Scanner(System.in);
            System.out.println("1,查看斐波那契数列使用long,int类型能够计算的最大n值");
            System.out.println("2,给定n值,求不同方法求得对应n值得斐波那契数值和计算时间");
            System.out.println("3,给定时间,求给定时间能用迭代算法计算到的最大n值");
            System.out.println("4,公式法F(n) = [n/sqrt(5)]求得有误差的最大n值");
            System.out.println("5.用矩阵相乘方法计算第n个斐波那契数");
            System.out.println("请选择你要执行的项目:");
            int chose=s.nextInt();
            switch(chose){...}
            System.out.println("是否返回首页?(1是,0退出程序)");
            con=s.nextInt();

        }while(con==1);

题目二:

public static int fbnq_dd(int n){
    int a[]=new int[n+1];
    a[0]=0;a[1]=1;int result=0;
    if(n==1) return 1;
    for(int i=2;i<n+1;i++){
        a[i]=a[i-1]+a[i-2];
        result=a[n];
    }
    return result;
}
public static int dd_int_compare(){
    int m=1;int n=1;int q=2;//0,1,1,2,3,5,8
    int count=3;
    while(n<q){
        m=n;
        n=q;
        q=m+n;
        count++;
    }
    return count-1;
}

题目三:

public static int fbnq_dg(int n){
    if(n==1||n==2) return 1;
    int result=0;
    if(n>2){
        result=fbnq_dg(n-1)+fbnq_dg(n-2);
    }
    return result;
}
public static int fbnq_dg_time(long time){
    long starttime_dg=System.currentTimeMillis();
    int i=3;
    long endtime_dg=0;
    while(endtime_dg<starttime_dg+time*1000){
        endtime_dg=System.currentTimeMillis();
        i++;
        fbnq_dg(i);
    }
    return i;
}

题目四:

System.out.println("先输入数值time,单位秒");
long time=s.nextLong();
int com_n=fbnq_dg_time(time);
int com_n_dd=fbnq_dd_time(time);
System.out.println(time+"秒能递归计算到第"+com_n+"个斐波那契数");
System.out.println(time+"秒能迭代计算到第"+com_n_dd+"个斐波那契数");
public static int fbnq_dg_time(long time){
    long starttime_dg=System.currentTimeMillis();
    int i=3;
    long endtime_dg=0;
    while(endtime_dg<starttime_dg+time*1000){
        endtime_dg=System.currentTimeMillis();
        i++;
        fbnq_dg(i);
    }
    return i;
}
public static int fbnq_dd_time(long time){
    long starttime_dg=System.currentTimeMillis();
    int i=3;
    long endtime_dg=0;
    while(endtime_dg<starttime_dg+time*1000){
        endtime_dg=System.currentTimeMillis();
        i++;
        fbnq_dd(i);
    }
    return i;
}
long starttime_dd=System.nanoTime();
int result=fbnq_dd(n);
                    long endtime_dd=System.nanoTime();
                    System.out.println("迭代的方式求得f(n):"+result);
                    System.out.println("迭代的方式所用时间为:"+(endtime_dd-starttime_dd)+"纳秒");

                    long starttime_dg=System.nanoTime();
                    int result2=fbnq_dg(n);  //递归运行的位置
                    long endtime_dg=System.nanoTime();
                    System.out.println("递归的方式求得f(n):"+result2);
                    System.out.println("递归的方式所用时间为:"+(endtime_dg-starttime_dg)+"纳秒");

题目五:

public static long fbnq_fn(int n){
    double temp =Math.sqrt(5.0);
    double d_temp=1/temp;
    double result=d_temp*(Math.pow(((1+temp)/2),n)-Math.pow(((1-temp)/2),n));
    return Math.round(result);
}
public static int max_wrong(){
    int i=1;
    while((int)fbnq_fn(i) == fbnq_dd(i)){
        i++;
    }
    return i;
}

题目六:

public static int fbnq_jz(int n){
    int x=0,y=1,z=1,p=2;
    int temp1=0;int temp2=0;
    for(int i=1;i<n;i++){
        temp1=x;
        x=x*0+y*1;
        y=temp1*1+y*1;
        temp2=z;
        z=z*0+p*1;
        p=temp2*1+p*1;
    }
    return y;
}

题目七:
递归:时间复杂度:O(2^N) 空间复杂度:O(N)

迭代:时间复杂度:O(N) 空间复杂度:O(1)

公式:时间复杂度:O(logn)

矩阵:时间复杂度:O(logn)

你可能感兴趣的:(算法,java,数据结构,动态规划)