【20170927】Java每日一练

来源:互联网 发布:网络嗅探器免费版 编辑:程序博客网 时间:2024/05/23 11:31

这里插播一个【回溯法求全排列】:

package qpl;import java.util.Scanner;public class qpl {    static int n;    static int count;    static boolean bUsed [] = new boolean[9];    static int result[] = new int [9];    static void qQl(int depth){        if(depth == n){            for(int i = 0;i < n; i++){                System.out.print(result[i]);                }            System.out.print("\t");            count++;            if(count % 6 == 0){                System.out.println();            }        }        for(int i = 1; i <= n;i++){            if(bUsed[i] == false){                bUsed[i] = true;                result[depth] = i;                qQl(depth+1);                bUsed[i] = false;            }        }

运行结果:

这里写图片描述

【斐波那契数列】
**【程序11】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?**

package p11;public class P11 {//斐波那契数列的特点就是:第一项和第二项为1,其余后面的每一项都等于前面两项之和。    public static void main(String[] arg){        int i;        long f1,f2;        f1 = 1;        f2 = 1;        for(i = 1;i <=20; i++){            System.out.println(f1 + "\t" + f2);            if(i % 2 == 0){                System.out.println();            }            f1 = f1 + f2;            f2 = f1 + f2;        }    }}

运行结果:

这里写图片描述

原创粉丝点击