Java敲几个小小练习

在这里插入图片描述
海底捞!

小练习

1、Scanner用法练习

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int suv = (int) (Math.random() * 100);
        while (sc.hasNext()) {
            System.out.println("请输入你想输入的数字:(1-100)");
            int set = sc.nextInt();
            if (set < suv) {
                System.out.println("猜小了!");
            } else if (set > suv) {
                System.out.println("猜大了!");
            } else {
                System.out.println("猜中了!");
            }
        }
        sc.close();
    }
第三行的random可以换一种用法,它的效果一样都是[0,100)随机数
        Random random = new Random();
        int toGuess = random.nextInt(100);

记得调用Scanner且用完后加入scanner.close解除调用

这里还加入了下面的代码,就是为了连续输入

        while (sc.hasNext()) {
            
        }

2、判断素数问题多种解法

2.1、基础解法

    public static boolean sun(int num) {
        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
         if (sun(7)){
             System.out.println("是素数!");
         }else {
             System.out.println("不是素数!");
         }
    }

这里的核心代码是:
下面两种也是基于这个代码进行调整

        for (int i = 2; i < num; i++) {
            
        }

2.2、进阶

这里将num换成num/2
因为一个数 n 可以写成ab的形式,不算1*n 则n/2>=a,b

public static boolean sun(int num) {
        for (int i = 2; i < num/2; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
         if (sun(7)){
             System.out.println("是素数!");
         }else {
             System.out.println("不是素数!");
         }
    }

2.3、最终 开根号法

Math.sqrt(num)
这时>=ab当中较小的那个

public static boolean sun(int num) {
        for (int i = 2; i < Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
         if (sun(7)){
             System.out.println("是素数!");
         }else {
             System.out.println("不是素数!");
         }
    }

3、 9*9乘法口诀

    public static void fun() {
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < i+1; j++) {
                System.out.print(j+"*"+i+"="+j*i+" " );
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        fun();
    }

4、俩正整数的最大公约数

在这里插入图片描述

public static int tim(int a,int b){
       int c = a%b;
       while (c!=0){
           a = b;
           b = c;
           c = a%b;
       }
       return b;
    }
    public static void main(String[] args) {
        System.out.println(tim(96, 268));
    }

5、水仙花数 全解法

public static void atm(){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int num = sc.nextInt();
            int count = 0;
            int net = num;
            while (net != 0) {
                count++;
                net /= 10;
            }
            net = num;
            int sum = 0;
            while (net != 0) {
                sum += Math.pow(net % 10, count);
                net /= 10;
            }
            if (sum == num) {
                System.out.println("所输入的是水仙花数:" + sum);
            } else {
                System.out.println("所输入的不是水仙花数!");
            }
        }
        sc.close();
    }

    public static void main(String[] args) {
        atm();
    }

解题思路:
1.先判断输入的这个数是几位数,这时候应该先找一个代替数

            while (net != 0) {
                count++;
                net /= 10;
            }

2.确定每一位是多少,然后根据水仙花数特点计算,这里的Math.pow(,)代表幂级数计算

            while (net != 0) {
                sum += Math.pow(net % 10, count);
                net /= 10;
            }

3.上面我加入的是while()输入控制台连续输入
如果想取某个范围内的所有水仙花数,如下:

public static void atm1(){
        for (int num = 0; num < 999_999; num++) {
            int count = 0;
            int net = num;
            while (net != 0) {
                count++;
                net /= 10;
            }
            net = num;
            int sum = 0;
            while (net != 0) {
                sum += Math.pow(net % 10, count);
                net /= 10;
            }
            if (sum == num) {
                System.out.println(sum);
            }
        }
    }
    public static void main(String[] args) {
        atm1();
    }

6、返回参数二进制中 1 的个数

6.1、普遍做法 无限右移(32封顶)

    public static int kpl(int n){
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if (((n>>i) & 1)!=0){
                count++;
            }
        }
        return count;
    }
    public static void main(String[] args) {
        System.out.println(kpl(7));
    }

6.2、无规则右移

每移动一次都会判断,效率更高

    public static int kpl1(int n){
        int count = 0;
        while (n!=0){
            if ((n&1)!=0){
                count++;
            }
            n = n >>> 1;
        }
        return count;
    }
    public static void main(String[] args) {
        System.out.println(kpl1(1));
    }

6.3、最优解法

    public static int kpl2(int n){
        int count = 0;
        while (n!=0){
            count++;
            n = n&(n-1);
        }
        return count;
    }
    public static void main(String[] args) {
        System.out.println(kpl2(7));
    }

7、判断一个数是不是2^k

    public static void kpl3(int n) {
        if ((n&(n-1))==0){
            System.out.println("这个数是2^k");
        }else {
            System.out.println("这个数不是2^k");
        }
    }

    public static void main(String[] args) {
        kpl3(8);
    }

8、二进制序列偶数位与奇数位

public class demo20220506 {
    public static void tt(int n){
        System.out.print("奇数位:");
        for (int i = 31; i >0; i-=2) {
            System.out.print(((n >> i)&1)+" ");
        }
        System.out.println();
        System.out.print("偶数位:");
        for (int i = 30; i >= 0; i-=2) {
            System.out.print(((n >> i)&1)+" ");
        }
    }
    public static void main(String[] args) {
        tt(7);
        输出结果:
        奇数位:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
        偶数位:0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
        Process finished with exit code 0
    }
  • 26
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 25
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值