Java入门练习题及其答案第一弹

Java入门练习题及其答案第一弹

素数打印

  1. 只能被1和自己整除
import java.util.Scanner;

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
    //这里通过if去判断1 或者是负数的情况
        int i = 2;
        for (; i < n ; i++) {
            if(n % i == 0){
                //System.out.println(i + "不是素数!");
                break;
            }
        }
        if(i == n) {
                System.out.println(n + "是素数!");
        }
    }
  1. n -> a*b

    2 – n/2

image-20221223212025213

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i = 2;
        for (; i <= n/2 ; i++) {
            if(n % i == 0){
                break;
            }
        }
        if(i > n/2) {
            System.out.println(n + "是素数!");
        }
    }
  1. 一定有一个数字小于等于根号n

    public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int i = 2;
            for (; i <= Math.sqrt(n) ; i++) {
                if(n % i == 0){
                    break;
                }
            }
            if(i > Math.sqrt(n)) {
                System.out.println(n + "是素数!");
            }
        }
    
    1. 打印1 – n间所有素数
import java.util.Scanner;
public class Measure {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int j = 1; j <= n; j++) {
            int i = 2;
            for (; i <= Math.sqrt(j); i++) {
                if (j % i == 0) {
                    break;
                }
            }
            if (i > Math.sqrt(j)) {
                System.out.println(j);
            }
        }
    }
}

乘法口诀表

import java.util.Scanner;
public class Measure {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i*j + " ");
            }
            System.out.println();
        }
    }
}

最大公约数

辗转相除法:除到余数为0

import java.util.Scanner;
public class Measure {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = a % b;
        while(c != 0) {
            a = b;
            b = c;
            c = a % b;
        }
        System.out.println(b);
    }
}

水仙花数

0~999间所有水仙花数(三位自逆数)

思路:image-20221224221702948

import java.util.Scanner;
public class Measure {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();//999999
        for (int i = 0; i < n; i++) {
            //判断i是几位数 求得每个位上是几 加起来== 原来的数字
            int count = 0;
            int tmp = i;
            while(tmp != 0) {
                count++;
                tmp = tmp / 10;
            }
            //tmp=0  i不变
            tmp = i;
            int sum = 0;
            while(tmp != 0) {
                sum += Math.pow(tmp % 10,count);
                tmp /= 10;
            }
            if(sum == i) {
                System.out.println(i);
            }
        }
    }
}

二进制中1的个数

import java.util.Scanner;
public class Measure {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = 0;
        for (int i = 0; i < 32; i++) {
            if(((n >>> i) & 1) != 0) {
                count++;
            }
        }
        System.out.println(count);
    }
}

另解

import java.util.Scanner;
public class Measure {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = 0;
        while(n != 0) {
            if((n & 1) != 0) {
                count++;
            }
            n = n >>> 1;
        }
        System.out.println(count);
    }
}

更高效的思路

image-20221224224604443

import java.util.Scanner;
public class Measure {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = 0;
        while(n != 0) {
            n = n & (n-1);
            count++;
        }
        System.out.println(count);
    }
}

二进制奇偶数位

 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        //偶数位
        for (int i = 31; i >= 1; i -= 2) {
            System.out.print(((n >> i) & 1) + " ");
        }
        System.out.println();
        for (int i = 30; i >= 0 ; i -= 2) {
            System.out.print(((n >> i) & 1) + " ");
        }
    }

结果image-20221224231133281

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值