溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java語法實例分析

發(fā)布時間:2022-05-05 16:19:56 來源:億速云 閱讀:124 作者:zzz 欄目:開發(fā)技術

本篇內容主要講解“Java語法實例分析”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Java語法實例分析”吧!

一,素數求解的n種境界

1.1,暴力循環(huán)求解

public class TestDemo220427 {
    public static void main(String[] args) {
//        這里以求取1~100之間的素數為例
        for(int i = 2;i <= 100;i++){//素數從2開始,所以從2開始產生到100的數
            int flg = 1;//假設是素數
            for(int j = 2;j < i;j++){
                if(i%j == 0){
                    flg = 0;
                }
            }
            if(flg == 1){
                System.out.println(i + "是素數!");
            }
        }
    }
}

1.2,試除前一半數

public class TestDemo220427 {
    public static void main(String[] args) {
//        這里以求取1~100之間的素數為例
        for(int i = 2;i <= 100;i++){//素數從2開始,所以從2開始產生到100的數
            int flg = 1;//假設是素數
            for(int j = 2;j < i/2;j++){
                if(i%j == 0){
                    flg = 0;
                }
            }
            if(flg == 1){
                System.out.println(i + "是素數!");
            }
        }
    }
}

可以發(fā)現,我們一個數都是可以拆成兩個數的乘法的,比如 16:可以是 1 16,2 * 8,4*4,可以看到,前半部分的數都是小于其自身的一半的,所以我們只需要檢測這前半部分數能否被其自身整除了,因為只要前半部分有的話,后半部分肯定有一個數與之對應相乘能夠得到自身,所以這就又減少了一半的工作量。*

1.3,試除小于自身開根號的數

import java.lang.Math;
public class TestDemo220427 {
    public static void main(String[] args) {
//        這里以求取1~100之間的素數為例
        for(int i = 2;i <= 100;i++){//素數從2開始,所以從2開始產生到100的數
            int flg = 1;//假設是素數
            for(int j = 2;j <= (int)(Math.sqrt(i));j++){
                if(i%j == 0){
                    flg = 0;
                }
            }
            if(flg == 1){
                System.out.println(i + "是素數!");
            }
        }
    }
}

還是剛才差不多的原理,只不過把范圍又縮小了,因為一個數拆分成兩個數的乘積的形式的話,前面的那個數不僅僅只是小于其自身的一半,其實根本上是不可能大于其開平方的值的,就比如16,其實前半部分的數不會大于4,因為大于4后可以看到不可能會有某個數能夠與另一個數乘了等于16了,當然2 * 8,8 * 2這只算前面一種就好了

1.4,在奇數中尋找

import java.lang.Math;
public class TestDemo220427 {
    public static void main(String[] args) {
//        這里以求取1~100之間的素數為例
        for(int i = 1;i <= 100;i += 2){//從1開始,產生到100的奇數
            int flg = 1;//假設是素數
            if(i == 1){
                System.out.println((i+1) + "是素數!");//2這里需要單拎出來考慮,比較特殊
                continue;
            }
            for(int j = 2;j <= (int)(Math.sqrt(i));j++){
                if(i%j == 0){
                    flg = 0;
                }
            }
            if(flg == 1){
                System.out.println(i + "是素數!");
            }
        }
    }
}

我們知道,除了2這個特例,所有的偶數不可能是素數,因為最起碼就能夠被2整除,所以在范圍內進行考慮的時候,就只需要檢測奇數就好了,就把外層循環(huán)的次數減少了。

其實還有方法可以繼續(xù)優(yōu)化,這里就不再給大家一一列舉了,如果大家有興趣的話可以去查查,很多博主寫的很詳細深入!

二,閏年問題

public class TestDemo220427 {
    public static boolean isleapYear(int year){
        if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
            return true;
        } else{
            return false;
        }
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入年份:");
        int year = scan.nextInt();
        boolean ret = isleapYear(year);
        if(ret == true){
            System.out.println(year + "是閏年!");
        }else{
            System.out.println(year + "不是閏年!");
        }
    }
}

這里就只需要知道閏年的判斷標準就可以很好的把題解出來。

三,求最大公約數以及最小公倍數

3.1,求最大公約數

import java.util.Scanner;
public class TestDemo220427 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        int m = 0;
        while((m = a%b) != 0){//輾轉相除法
            a = b;
            b = m;
        }
        System.out.println(b);
    }
}

3.2,求最小公倍數

import java.util.Scanner;
public class TestDemo220427 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        int b = scan.nextInt();
        for(int i = 1;i > 0;i++){
            if((a*i)%b == 0){
                System.out.println("最小公倍數:" + a*i);
                break;
            }
        }
    }
}

其實還有一個公式,假設最大公約數是m,則最小公倍數是 (a*b)/m。

四,自冪數問題

import java.lang.Math;
public class TestDemo220427 {
    public static boolean isNarnum(int num,int count){
        int sum = 0;
        int tmp = num;
        while(tmp != 0){
            sum += Math.pow(tmp%10,count);
            tmp /= 10;
        }
        if(sum == num){
            return true;
        }else{
            return false;
        }
    }
    public static void main(String[] args) {
//        判斷一個數是不是自冪數
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入一個數:");
        int num = scan.nextInt();
        int count = 0;
        int tmp = num;
        while(tmp != 0){
            count++;
            tmp /= 10;
        }
        boolean ret = isNarnum(num,count);
        if(ret == true){
            System.out.println(num + "是一個" + count +"位自冪數!");
        }else{
            System.out.println(num + "不是自冪數!");
        }
    }
}

五,統計二進制位中1的個數

5.1,循環(huán)右移按位與1

import java.util.Scanner;
public class TestDemo220427 {
    public static int getOnecount(int num){
        int count = 0;
        while(num != 0){//右移后不為0就繼續(xù)統計
            if((num& 1) == 1){
                count++;
            }
            num = num >> 1;
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入一個數:");
        int num = scan.nextInt();
        int ret =  getOnecount(num);
        System.out.println(num + "的二進制位中1的個數 :" + ret);
    }
}

注意:這段代碼是有bug的,因為對于負數是統計不了的,負數的二進制最高符號位為1,右移補符號位那就是一直在高位補1,那循環(huán)就死循環(huán)了。

解決方法:num = num >> 1 &mdash;&mdash;> 改成 num = num >>> 1,用無符號右移,這樣高位就只會補0,對于正數負數都適用。

拓展:可能有人會問,既然可以右移,那為啥不能左移?

答案是 : 確實可以左移,但是不推薦,效率太低。

public class TestDemo220427 {
    public static int getOnecount(int num){
        int count = 0;
        for(int i = 0;i < 32;i++){
            if((num & (1 << i)) != 0){
                count++;
            }
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入一個數:");
        int num = scan.nextInt();
        int ret =  getOnecount(num);
        System.out.println(num + "的二進制位中1的個數 :" + ret);
    }
}

這個時候就不是把這個數去左移了,而是把1左移,然后去與這個數按位與,因為這樣的結果就只有可能是0或者非0,非0就表示1左移后的結果的1所在的位置對應的這個數的位置上是1,所以這個時候就統計一下。這樣也可以解決問題,但是你必須得左移32次,因為你不知道這個數前面到底有多少1,只能所有的都比對完。

5.2,n &(n-1)消除1的原理

import java.util.Scanner;
public class TestDemo220427 {
    public static int getOnecount(int num){
        int count = 0;
        while(num != 0){
            num = num&(num-1);
            count++;
        }
        return count;
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入一個數:");
        int num = scan.nextInt();
        int ret =  getOnecount(num);
        System.out.println(num + "的二進制位中1的個數 :" + ret);
    }

這種方法正數負數都可以用,并且效率很高,每次按位與num-1 一次,就會消掉一個1。

擴展:用這個方法判斷某一個數是不是2的k次方。

import java.util.Scanner;
public class TestDemo220427 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("請輸入一個數:");
        int num = scan.nextInt();
        if((num & (num-1)) == 0){
            System.out.println("是2的k次方數!");
        }else{
            System.out.println("不是2的k次方數!");
        }
    }
}

到此,相信大家對“Java語法實例分析”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI