栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java练习—方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java练习—方法

✨作者:@小孙的代码分享

✨专栏:《Java SE》

✨送给各位的一句话:空杯心态 才能学到新知

✨希望大家看完这些题目有所收获,别忘了,点赞+评论!

1.判断一个数字是否是素数(三个方法)

方法一 :

用除了1和它本身外的数字除以它,能被整除的就不是素数。

public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int a=scan.nextInt();
        int i=2;
        for (;i

方法二:

优化算法——(i<=a/2)

 

public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int a=scan.nextInt();
        int i=2;
        for (;i<=a/2;i++) {
            if(a%i==0){
                System.out.println("不是素数!");
                break;
            }
        }
        if (i>a/2){
            System.out.println("是素数!");
        }
    }

方法三:

提升算法效率——sqrt()

 

public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);
        int a=scan.nextInt();
        int i=2;
        for (;i<=Math.sqrt(a);i++) {
            if(a%i==0){
                System.out.println("不是素数!");
                break;
            }
        }
        if (i>Math.sqrt(a)){
            System.out.println("是素数!");
        }
    }

2.求最大公倍数和最小公倍数

tips:计算最大公约数是用辗转相除法,最小公倍数是通过两数乘积除以最大公约数。

直接给大家上代码:

public class Test {
public static void main(String[] args) {
    //辗转相除法
    Scanner scanner = new Scanner(System.in);
    int a = scanner.nextInt();
    int b = scanner.nextInt();
    int c = a%b;
    int b1 = b;
    while (c != 0) {
        a = b1;
        b1 = c;
        c = a%b1;
    }
    System.out.println(b1+" 是最大公约数!");
    //知道最大公约数,算最小公倍数
    //可以将两数相乘再除以最大公约数
    System.out.println(a*b/b1+"最小公倍数! ");
    }
}

3.求出0~999之间的所有“水仙花数”并输出

先带大家了解了解水仙花数;

 然后就直接上代码:

public class Test {
public static void main(String[] args) {
    for (int i = 1; i < 999999; i++) {
        int count=0;//位数
        int tmp = i;
        while(tmp != 0){
            count++;
            tmp /= 10;
            
        }
        //tmp ==0; count==计算出的位数;
        tmp = i;
        int sum = 0;
        while(tmp != 0){
            sum += Math.pow(tmp%10,count);
            tmp /= 10;
        }
        //此时tmp中存放的就是每一位的count次方和
        if(sum == i){
            System.out.println(i);
        }
    }
}
}

4.写出一个函数返回参数二进制中1的个数(三个方法)

方法一:

 

public class Test {
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);
}
}

但是这种方法不高效,需要移动32次,那么可不可以优化一下,使算法效率提高

方法二:

 

public class Test{
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);
    }
}

大家可以发现我们这里使用了一个>>>(无符号右移),就可以输入负数了

方法三:

 

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);
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853910.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号