JAVA经典算法40题

刷题开始!

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

总结规律: 1 1 2 3 5 8 13 —
其实就是一个数列,前一项 + 后一项 = 现在这一项,用递归最快,最简单

public class Demo01 {

    public static void main(String[] args) {

        for(int i = 1; i < 12; i++) {
            System.out.print("第" + i + "月" + "有  " + tuzi(i) + "只\n");
        }
    }

    public static int tuzi(int month) {

        if (month == 1 || month == 2) {
            return 1;
        } 

        return tuzi(month-1) + tuzi(month-2);
    }
}
题目2:判断101-200之间有多少个素数,并输出所有素数
public class Demo2 {



    public static void main(String[] args) {

        for (int i = 101; i < 200; i++) {
            if (isSuShu(i)) {
                System.out.print(i + " ");
            }
        }
    }


    public static boolean isSuShu(int x) {

        boolean flag = false;
        for (int i = 2; i <= Math.sqrt(x); i++) {
            if(x%i==0) {
                flag =  false;
                break;
            }

            flag =  true;
        }

        return flag;
    }

}

注意一下,在Math.sqrt中,要<=, 不然会出问题

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


    public static void main(String[] args) {

        for (int i = 100; i < 1000; i++) {
            int b = i /100;
            int s = i/10%10;
            int g = i%10;
            if (i == (b*b*b + s*s*s + g*g*g)) {
                System.out.print( i + " ");
            }
        }
    }

}

主要是对三位数进行拆分位数

题目4:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
public class Demo4 {

    public static void main(String[] args) {

        int value  =  new Scanner(System.in).nextInt();
        int flag = 0;
        for (int i = 2; i <= value; i++) {

            while(value % i ==0) {
                flag++;
                if (flag == 1) {
                    System.out.print(value + "=" + i);
                }else {
                    System.out.print("*" + i);
                }

                value = value/i;
            }
        }
    }   
}
题目5:利用条件运算符的嵌套来完成此题:学习成绩> =90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
public class Demo5 {


    public static void main(String[] args) {
        System.out.println("请输入学成成绩:");

        int value = new Scanner(System.in).nextInt();
        String str = "";
        if (value >= 90) {
            str = "A";
        } else if (value >= 60 && value < 89) {
            str = "B";
        } else {
            str = "C";
        }

        System.out.println("成绩等级为: " + str);
    }

}
题目6:输入两个正整数m和n,求其最大公约数和最小公倍数。
public class Demo6 {


    public static void main(String[] args) {

        System.out.println("请输入十个数字: ");
        Scanner input = new Scanner(System.in);

        int m = input.nextInt();
        int n = input.nextInt();
        int t = n*m;

        if(n > m) {
            int temp = n;
            n = m;
            m = temp;
        }

        int temp;
        while( m%n != 0) {

            temp = m%n;
            m = n;
            n = temp;
        }

        System.out.println("最大公约数" + n   + " 最小公倍数" + t/n );
    }

}

个人觉得这题,有点迷糊,主要是概念不清
点击一下 : 概念

题目7:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
public class Demo7 {


    public static void main(String[] args) {

        System.out.println("请输入一行字符: ");
        String data = new Scanner(System.in).nextLine();

        int y = 0;
        int s = 0;
        int k = 0;
        int q = 0;
        char[] cs = data.toCharArray();
        for (int i = 0; i < cs.length; i++) {
            if (cs[i] >='A' && cs[i] <='z') {
                y++;
            } else if (cs[i] >= '0' && cs[i] <= '9') {
                s++;
            } else if (cs[i] == ' ') {
                k++;
            } else {
                q++;
            }
        }

        System.out.println("字母个数: " + y);
        System.out.println("空格个数: " + k);
        System.out.println("数字个数: " + s);
        System.out.println("其它个数: " + q);

    }
}

很多人,用正则表达式,但我觉得我这个是最简单的,直接利用字符串转换成字节数组,再一个一个判断

题目8:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
public class Demo8 {

    public static void main(String[] args) {

        System.out.println("请输入多少项,由你决定 : ");
        int n = new Scanner(System.in).nextInt();
        long sum = 0;
        long s = 0;
        for (int i = 0; i < n; i++) {

            sum = sum * 10 + n;
            s = s + sum;
        }

        System.out.println("数值为: "  + s);
    }
}

主要是利用x不断的*10 倍再加上上一次的数,这个是小技巧

题目9:一个数如果恰好等于它的因子之和,这个数就称为 “完数 “。例如6=1+2+3.编程 找出1000以内的所有完数。
public class Demo9 {


    public static void main(String[] args) {

        for (int i = 1; i < 1000; i++) {
            if (i == yinZi(i)) {
                System.out.println(i);
            }
        }
    }


    public static int yinZi(int d) {

        int sum = 0;
        for (int i = 1; i < d; i++) {
            if (d % i == 0) {
                sum += i;
            }
        }

        return sum;
    }
}
题目10:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?
public class Demo10 {

    public static void main(String[] args) {

        double last = 100;
        double sum = 0;
        for (int i = 0; i < 10; i++) {
            sum = sum + last;
            last = last/2;

        }

        System.out.println("共经过: " + sum + "米");
        System.out.println("第10次反弹 " + last + "米");

    }
}

关键是题目看懂,要注意用double, 因为有小数,不然结果最后为0了,别问我怎么知道

题目11:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
public class Demo11 {


    public static void main(String[] args) {

        for (int i = 1 ; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                for (int k = 1; k <= 4; k++) {
                    if (i!=j && i!=k &&j !=k ) {
                        System.out.println(i +"" + j + "" + k);
                    }
                }
            }
        }
    }
}

这是水仙花典型题型,不说了,太简单了

题目12:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润,求应发放奖金总数?
public class Demo12 {



    public static void main(String[] args) {
        System.out.println("请输入月利润: ");
        int money = new Scanner(System.in).nextInt();
        double sum = 0;
        if (money <= 10) {
            sum = sum * 0.1; 
        } else if (money > 10 && money <= 20) {
            sum = 10 * 0.1 + (money - 10) * 0.075;
        } else if (money > 20 && money <= 40) {
            sum = 10 * 0.1 + 10 * 0.075 + (money - 20) * 0.05;
        } else if (money > 40 && money <= 60) {
            sum = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (money - 40) * 0.03;
        } else if(money > 60 && money <= 100) {
            sum = 10 * 0.1 + 10 * 0.075 + 20 *0.05 + 40 * 0.03 + (money - 60) * 0.015;
        } else if (money > 100) {
            sum = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 40 * 0.03 + 60 * 0.015 + (money - 100) * 0.01;
        }

        System.out.println("sum " + sum + "万");
    }
}

主要是数学思维,要小心仔细

题目13:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
public class Demo13 {

    public static void main(String[] args) {

        long a;
        long b;
        for (int i = 0; i < 100000; i++) {
            a = (long)Math.sqrt(i+100);
            b = (long)Math.sqrt(i+100+168);
            if ( (a*a == (i+100)) && (b*b == (i + 100 + 168) )) {
                System.out.println(i);
            }
        }



    }

}
题目14:输入某年某月某日,判断这一天是这一年的第几天?
public class Demo14 {

    public static void main(String[] args) {
        System.out.println("输入年 月日 : ");
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        int month = input.nextInt();
        int day = input.nextInt();
        int sum = 0;
        int te = 0;
        if (isR(year)) {
            te = 29;
        } else {
            te = 28;
        }
        switch (month) {
        case 2:
            sum = 31;
            break;
        case 3:
            sum = 31 + te;
            break;
        case 4:
            sum = 31 + te + 31;
            break;
        case 5:
            sum = 31 + te + 31 + 30;
            break;
        case 6:
            sum = 31 + te + 31 + 30 + 31;
            break;
        case 7:
            sum = 31 + te + 31 + 30 + 31 + 30;
            break;
        case 8:
            sum = 31 + te + 31 + 30 + 31 + 30 + 31;
            break;
        case 9:
            sum = 31 + te + 31 + 30 + 31 + 30 + 31 + 31;
            break;
        case 10:
            sum = 31 + te + 31 + 30 + 31 + 30 + 31 + 31 + 30;
            break;
        case 11:
            sum = 31 + te + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
            break;
        case 12:
            sum = 31 + te + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
            break;

        default:
        }
        System.out.println("第" + (sum + day) +"天");
    }

    public static boolean isR(int year) {

        boolean flag = false;
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
            flag = true;
        }

        return flag;
    }
}

原谅我的笨方法

题目15:输入三个整数x,y,z,请把这三个数由小到大输出。
public class Demo15 {

    public static void main(String[] args) {
        System.out.println("请输入3个数字: ");
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        System.out.println("三个数字为: " + a + " " + b + " "  +c);

        if(a > b) {
            a = a^b;
            b = a^b;
            a = a^b;
        }

        if(a > c) {
            a = a^c;
            c = a^c;
            a = a^c;
        }

        if (b > c) {
            b = b^c;
            c = b^c;
            b = b^c;
        }

        System.out.println("之后的数值: " + a + " " + b + " " + c);
    }

}

呜呜,好累,大家让我休息下,代码已发到我的github,大家有需要,可以点击下载
点击基础试题下载

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值