JAVA经典算法,每日一题

来源:互联网 发布:淘宝网怎么加好友呢 编辑:程序博客网 时间:2024/05/17 08:54

【一】题目:古典问题:有一对兔子,从出生后第三个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死们每个月兔子的总数为多少?

梳理思路:首先先计算出每个月兔子的数量,摸清规律再去编码实现
第一个月1对
第二个月1对
第三个月2对(这对祖宗兔子生了一对,这一对命名为第一代,所以是1+1)
第四个月3对(这对祖宗兔子又生了一对,这一代命名为二代,2+1)
第四个月5对(祖宗兔子又生的一对+第一代刚好长到第三个月也生的一对命名第三代,所以是3+2 = 5)
第五个月8对(祖宗兔子又生的一对+第一代又生的一对+第二代又生的一对,5+3 = 8)
第六个月13对(祖宗兔子又生的一对+第一代又生第一对+第二代又生的一对+第三代又生的一对+第一代生的小崽子又生的1对 ,所以是8+5 = 13)

到此为止,可以发现规律,这个1,1,2,3,5,8,13这个数列从第三项开始,每一项都等于前两项之和。这也就是斐波那契数列。
于是开始编程
<一>直观的方法

public class Program1 {    public static void main(String[] args) {        // TODO Auto-generated method stub        int f1 = 1;        int f2 = 1;        int f;        int M =24;        System.out.println("第1个月的兔子为 :1对");        System.out.println("第2个月的兔子为 :1对");        for (int i = 3; i <=M; i++) {            f = f2;            f2 = f1+f2;            f1 = f;            System.out.println("第" + i+ "个月的兔子为 :"+f2+"对");        }    }}

<二>采用递归的方法

public class Program2 {    public static void main(String[] args) {        for (int i = 1; i <= 24; i++) {            System.out.println("第" + i+ "个月的兔子为 :"+f(i)+"对");        }    }    public static int f(int x) {        if (x == 1 || x == 2) {            return 1;        } else {            return f(x - 1) + f(x - 2);        }    }}

<二>题目:判断101-200之间有多少个素数,并输出所有的素数。

首先分析一下,什么是素数。素数又叫质数,定义为在大于1的自然数中,除了1和它本身以外不再有其他因数的数。换到代码语言里来说,用一个数分别去除2,3,4,..一直到sqrt(这个数),sqrt是Math类中的方法,是求平方根。如果能被整除则表明此数不是素数,反之是素数。

public class Program4 {    public static void main(String[] args) {        int count = 0;        // 判断素数的方法,用一个数分别去除2到sqrt(这个数),如果能被整除就表明此数不是质数,返只是素数        for (int i = 101; i < 200; i++) {            boolean b = false;            for (int j = 2; j <= Math.sqrt(i); j++) {                if (i % j == 0) {                    b = false;                    break;                } else {                    b = true;                }            }            if (b == true) {                count++;                System.out.println(i);            }        }        System.out.println("素数的个数是:" + count);    }}

<三>题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153 = 1的三次方+5的三次方+3的三次方。

public class program {    public static void main(String[] args) {        int geWei, shiWei, baiWei;        for (int m = 101; m < 1000; m++) {            baiWei = m/100;            shiWei = m%100/10;            geWei = m%10;            if((baiWei *baiWei *baiWei +shiWei *shiWei *shiWei +geWei *geWei *geWei )==m){                System.out.println(m+"是一个水仙花数");            }        }    }}

<四>题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5

程序分析:对n进行分解质因数,应先找到一个最小的质因数看,然后按下述步骤完成:

(1) 如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出结果即可。
(2) 如果n >=k,但能被k整除,则应打印出k的值,并用n除以k的值作为新的正整数n,然后重复执行第一步。
(3) 如果n不能被k整除,则k+1作为k的值,重复执行第一步。
代码如下:

public class program6 {    public static void main(String[] args) {        Scanner s = new Scanner(System.in);        System.out.println("请输入一个正整数:");        int n = s.nextInt();        int k = 2;        System.out.print(n + "=");        while (k <= n) {            if (k == n) {                System.out.print(n);                break;            } else if (n % k == 0) {                System.out.print(k + "*");                n = n / k;            } else {                k++;            }        }    }}

<五> 题目:l利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分一下的用C表示。

public class Program7 {    public static void main(String[] args) {        int x;        char grade;        Scanner s = new Scanner(System.in);        System.out.println("请输入一个成绩");        x = s.nextInt();        grade = x >= 90 ? 'A' : x >= 60 ? 'B' : 'C';        System.out.println("等级为:" + grade);    }}

<六>题目:题目:输入两个正整数m和n,求其最大公约数和最小公倍数。

  • 分析在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。
public class Program8 {    public static void main(String[] args) {        int a, b, m;        Scanner scanner = new Scanner(System.in);        System.out.println("请输入一个数");        a = scanner.nextInt();        System.out.println("请再输入一个数");        b = scanner.nextInt();        deff d = new deff();        m = d.deff(a, b);        System.out.println("最大公约数"+ m);        int n = a*b/m;        System.out.println("最小公倍数"+ n);    }}class deff {    public int deff(int x, int y) {        int t;        if (x < y) {// 将x,y交换            t = x;            x = y;            y = t;        }        while (y != 0) {            if (x == y)                return x;            else {                int k = x % y;                x = y;                y = k;            }        }        return x;    }}

【程序8】 题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。

public class Test8 {    public static void main(String[] args) throws IOException {        int s = 0;        int n;        int t = 0;        System.out.println("请输入a的值");        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String input = br.readLine();        n = Integer.parseInt(input);        for (int i = 0; i < n; i++) {            t = t * 10 + n;            s = s + t;        }        System.out.println(s);    }}
1 0
原创粉丝点击