JAVA之经典算法一

来源:互联网 发布:摄像机内存卡数据恢复 编辑:程序博客网 时间:2024/05/21 17:16

程序1:有一对兔子,从出生后第 3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21….

public class Demo1 {    public static void main(String args[]) {        math mymath = new math();        for (int i = 1; i <= 20; i++)            System.out.println(mymath.f(i));    }    static class math {        public int f(int x) {            if (x == 1 || x == 2)                return 1;            else                return f(x - 1) + f(x - 2);        }    }}

程序2:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

public class Demo2 {    public static void main(String args[]) {    boolean flag = true;    int primeNum = 0;        for(int i = 101 ; i < 201 ; i++){        flag = isPrime(i);        if(flag == true){            //是素数            System.out.println("101-200之间的素数有:" + i);            primeNum++;        }    }        System.out.println("101-200之间的素数数量共有:"+primeNum);}    public static boolean isPrime(int x){        boolean flag = true;        for(int i = 2 ; i <= Math.sqrt(x) ; i++){            if(x % i == 0){                flag = false;   //不是素数            }        }        return flag;    }}

程序3:打印出所有的 水仙花数 ,所谓 水仙花数 是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个 水仙花数 ,因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。

方法1:

public class Demo3 {    public static void main(String args[]) {        //方法一        for (int num=100;num<1000;num++)        {            int gw=num%10;            int sw=num/10%10;            int bw=num/100%10;            if (gw*gw*gw+sw*sw*sw+bw*bw*bw==num)            {                System.out.println(num);            }        }    }}

方法2:

public class Demo3 {    public static void main(String args[]) {        int n=0,x,y;        for (int i=1; i<=9; i++) {            for (int j=0; j<=9; j++) {                for (int k=0; k<=9; k++) {                    x=i*i*i+j*j*j+k*k*k;                    y=i*100+j*10+k;                    if (x==y) {                        n++;                        System.out.print(y+" ");                    }                }            }        }        System.out.print("三位数的水仙花数一共有: "+n+" 个");    }}

程序4:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

public class Demo4 {    public static void main(String[] args) {        System.out.println("请输人数n:");        Scanner in = new Scanner(System.in);        int n = in.nextInt();        boolean[] arr = new boolean[n];        for (int i = 0; i < arr.length; i++) {            arr[i] = true; //下标为TRUE时说明还在圈里        }        int leftCount = n;        int countNum = 0;        int index = 0;        while (leftCount > 1) {            if (arr[index] == true) { //当在圈里时                countNum++;  //报数递加                if (countNum == 3) { //报数为3时                    countNum = 0; //从零开始继续报数                    arr[index] = false; //此人退出圈子                    leftCount--; //剩余人数减一                }            }            index++; //每报一次数,下标加一            if (index == n) { //是循环数数,当下标大于n时,说明已经数了一圈,                index = 0; //将下标设为零重新开始。            }        }        for (int i = 0; i < n; i++) {            if (arr[i] == true) {                System.out.println(i);            }        }    }}

程序5:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个, 这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

方法1:

public class Demo5 {    static int ts = 0;// 桃子总数    static int fs = 1;// 记录分的次数    static int hs = 5;// 猴子数    static int tsscope = 5000;// 桃子数的取值范围,太大容易溢出。    public static int fT(int t) {        if (t == tsscope) {            // 当桃子数到了最大的取值范围时取消递归            System.out.println("结束");            return 0;        } else {            if ((t - 1) % hs == 0 && fs <= hs) {                if (fs == hs) {                    System.out.println("桃子数=" + ts + "时满足分桃条件");                }                fs += 1;                return fT((t - 1) / 5 * 4);// 返回猴子拿走一份后的剩下的总数            } else {                // 没满足条件                fs = 1;// 分的次数重置为1                return fT(ts += 1);// 桃子数加+1            }        }    }

方法2:

 public  class Demo5 {        public static void main(String[] args) {            int sum = 0;            for (int i = 6; ; i++) {// 最少6个分最后一次                sum = i;// 桃子数                for (int j = 0; j < 5; j++) {// 分的次数循环                    if ((sum - 1) % 5 == 0 && j < 5) {// 如果扔一个后能均分5份,继续分                        sum = (sum - 1) / 5 * 4;// 每分一次剩余桃子数                        if (j == 4) {// 如果已分5次,且仍能除尽,输出,退出程序                            System.out.println(i);                            System.exit(0);                        }                    }                }            }        }    }
原创粉丝点击