Java實(shí)例精煉掌握語(yǔ)法
今天的文章主要是總結(jié)了下幾個(gè)基礎(chǔ)的編程問(wèn)題,問(wèn)題都不是很難,但是每次總結(jié)都還是有不一樣的收獲,同樣呢也能夠讓自己更加熟悉Java的語(yǔ)法,畢竟C語(yǔ)言學(xué)的有點(diǎn)久,一下子揮之不去哈哈!??????
一,素?cái)?shù)求解的n種境界
1.1,暴力循環(huán)求解
public class TestDemo220427 { public static void main(String[] args) { // 這里以求取1~100之間的素?cái)?shù)為例 for(int i = 2;i <= 100;i++){//素?cái)?shù)從2開始,所以從2開始產(chǎn)生到100的數(shù) int flg = 1;//假設(shè)是素?cái)?shù) for(int j = 2;j < i;j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素?cái)?shù)!"); } } } }
1.2,試除前一半數(shù)
public class TestDemo220427 { public static void main(String[] args) { // 這里以求取1~100之間的素?cái)?shù)為例 for(int i = 2;i <= 100;i++){//素?cái)?shù)從2開始,所以從2開始產(chǎn)生到100的數(shù) int flg = 1;//假設(shè)是素?cái)?shù) for(int j = 2;j < i/2;j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素?cái)?shù)!"); } } } }
可以發(fā)現(xiàn),我們一個(gè)數(shù)都是可以拆成兩個(gè)數(shù)的乘法的,比如 16:可以是 1 16,2 * 8,4*4,可以看到,前半部分的數(shù)都是小于其自身的一半的,所以我們只需要檢測(cè)這前半部分?jǐn)?shù)能否被其自身整除了,因?yàn)橹灰鞍氩糠钟械脑?,后半部分肯定有一個(gè)數(shù)與之對(duì)應(yīng)相乘能夠得到自身,所以這就又減少了一半的工作量。*
1.3,試除小于自身開根號(hào)的數(shù)
import java.lang.Math; public class TestDemo220427 { public static void main(String[] args) { // 這里以求取1~100之間的素?cái)?shù)為例 for(int i = 2;i <= 100;i++){//素?cái)?shù)從2開始,所以從2開始產(chǎn)生到100的數(shù) int flg = 1;//假設(shè)是素?cái)?shù) for(int j = 2;j <= (int)(Math.sqrt(i));j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素?cái)?shù)!"); } } } }
還是剛才差不多的原理,只不過(guò)把范圍又縮小了,因?yàn)橐粋€(gè)數(shù)拆分成兩個(gè)數(shù)的乘積的形式的話,前面的那個(gè)數(shù)不僅僅只是小于其自身的一半,其實(shí)根本上是不可能大于其開平方的值的,就比如16,其實(shí)前半部分的數(shù)不會(huì)大于4,因?yàn)榇笥?后可以看到不可能會(huì)有某個(gè)數(shù)能夠與另一個(gè)數(shù)乘了等于16了,當(dāng)然2 * 8,8 * 2這只算前面一種就好了
1.4,在奇數(shù)中尋找
import java.lang.Math; public class TestDemo220427 { public static void main(String[] args) { // 這里以求取1~100之間的素?cái)?shù)為例 for(int i = 1;i <= 100;i += 2){//從1開始,產(chǎn)生到100的奇數(shù) int flg = 1;//假設(shè)是素?cái)?shù) if(i == 1){ System.out.println((i+1) + "是素?cái)?shù)!");//2這里需要單拎出來(lái)考慮,比較特殊 continue; } for(int j = 2;j <= (int)(Math.sqrt(i));j++){ if(i%j == 0){ flg = 0; } } if(flg == 1){ System.out.println(i + "是素?cái)?shù)!"); } } } }
我們知道,除了2這個(gè)特例,所有的偶數(shù)不可能是素?cái)?shù),因?yàn)樽钇鸫a就能夠被2整除,所以在范圍內(nèi)進(jìn)行考慮的時(shí)候,就只需要檢測(cè)奇數(shù)就好了,就把外層循環(huán)的次數(shù)減少了。
其實(shí)還有方法可以繼續(xù)優(yōu)化,這里就不再給大家一一列舉了,如果大家有興趣的話可以去查查,很多博主寫的很詳細(xì)深入!
二,閏年問(wèn)題
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("請(qǐng)輸入年份:"); int year = scan.nextInt(); boolean ret = isleapYear(year); if(ret == true){ System.out.println(year + "是閏年!"); }else{ System.out.println(year + "不是閏年!"); } } }
這里就只需要知道閏年的判斷標(biāo)準(zhǔn)就可以很好的把題解出來(lái)。
三,求最大公約數(shù)以及最小公倍數(shù)
3.1,求最大公約數(shù)
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){//輾轉(zhuǎn)相除法 a = b; b = m; } System.out.println(b); } }
3.2,求最小公倍數(shù)
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("最小公倍數(shù):" + a*i); break; } } } }
其實(shí)還有一個(gè)公式,假設(shè)最大公約數(shù)是m,則最小公倍數(shù)是 (a*b)/m。
四,自冪數(shù)問(wèn)題
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) { // 判斷一個(gè)數(shù)是不是自冪數(shù) Scanner scan = new Scanner(System.in); System.out.println("請(qǐng)輸入一個(gè)數(shù):"); 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 + "是一個(gè)" + count +"位自冪數(shù)!"); }else{ System.out.println(num + "不是自冪數(shù)!"); } } }
五,統(tǒng)計(jì)二進(jìn)制位中1的個(gè)數(shù)
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ù)統(tǒng)計(jì) 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("請(qǐng)輸入一個(gè)數(shù):"); int num = scan.nextInt(); int ret = getOnecount(num); System.out.println(num + "的二進(jìn)制位中1的個(gè)數(shù) :" + ret); } }
注意:這段代碼是有bug的,因?yàn)閷?duì)于負(fù)數(shù)是統(tǒng)計(jì)不了的,負(fù)數(shù)的二進(jìn)制最高符號(hào)位為1,右移補(bǔ)符號(hào)位那就是一直在高位補(bǔ)1,那循環(huán)就死循環(huán)了。
解決方法:num = num >> 1 ——> 改成 num = num >>> 1,用無(wú)符號(hào)右移,這樣高位就只會(huì)補(bǔ)0,對(duì)于正數(shù)負(fù)數(shù)都適用。
拓展:可能有人會(huì)問(wèn),既然可以右移,那為啥不能左移?
答案是 : 確實(shí)可以左移,但是不推薦,效率太低。
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("請(qǐng)輸入一個(gè)數(shù):"); int num = scan.nextInt(); int ret = getOnecount(num); System.out.println(num + "的二進(jìn)制位中1的個(gè)數(shù) :" + ret); } }
這個(gè)時(shí)候就不是把這個(gè)數(shù)去左移了,而是把1左移,然后去與這個(gè)數(shù)按位與,因?yàn)檫@樣的結(jié)果就只有可能是0或者非0,非0就表示1左移后的結(jié)果的1所在的位置對(duì)應(yīng)的這個(gè)數(shù)的位置上是1,所以這個(gè)時(shí)候就統(tǒng)計(jì)一下。這樣也可以解決問(wèn)題,但是你必須得左移32次,因?yàn)槟悴恢肋@個(gè)數(shù)前面到底有多少1,只能所有的都比對(duì)完。
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("請(qǐng)輸入一個(gè)數(shù):"); int num = scan.nextInt(); int ret = getOnecount(num); System.out.println(num + "的二進(jìn)制位中1的個(gè)數(shù) :" + ret); }
這種方法正數(shù)負(fù)數(shù)都可以用,并且效率很高,每次按位與num-1 一次,就會(huì)消掉一個(gè)1。
擴(kuò)展:用這個(gè)方法判斷某一個(gè)數(shù)是不是2的k次方。
import java.util.Scanner; public class TestDemo220427 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請(qǐng)輸入一個(gè)數(shù):"); int num = scan.nextInt(); if((num & (num-1)) == 0){ System.out.println("是2的k次方數(shù)!"); }else{ System.out.println("不是2的k次方數(shù)!"); } } }
到此這篇關(guān)于Java實(shí)例精煉掌握語(yǔ)法的文章就介紹到這了,更多相關(guān)Java實(shí)例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java消息隊(duì)列-Spring整合ActiveMq
本篇文章主要介紹了詳解Java消息隊(duì)列-Spring整合ActiveMq ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02Java虛擬機(jī)執(zhí)行引擎知識(shí)總結(jié)
這篇文章主要介紹了有關(guān)Java虛擬機(jī)執(zhí)行引擎的知識(shí),文中實(shí)例簡(jiǎn)單易懂,方便大家更好的學(xué)習(xí),有興趣的朋友可以了解下2020-06-06解決IDEA修改 .vmoptions 文件后導(dǎo)致無(wú)法啟動(dòng)的問(wèn)題
這篇文章主要介紹了解決IDEA修改 .vmoptions 文件后導(dǎo)致無(wú)法啟動(dòng)的問(wèn)題,需要的朋友可以參考下2020-12-12Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了Java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Netty事件循環(huán)主邏輯NioEventLoop的run方法分析
這篇文章主要介紹了Netty事件循環(huán)主邏輯NioEventLoop的run方法分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03SpringBoot與單元測(cè)試JUnit的結(jié)合操作
這篇文章主要介紹了SpringBoot與單元測(cè)試JUnit的結(jié)合操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Java將對(duì)象保存到文件中/從文件中讀取對(duì)象的方法
下面小編就為大家?guī)?lái)一篇Java將對(duì)象保存到文件中/從文件中讀取對(duì)象的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12