常用一些Java基礎算法積累

前幾天去北京參加了“藍橋杯”比賽,結果拿了二等獎,雖然還沒達到我的目標,但這也算是一個比較美好的結局吧。爲了準備這次比賽,積累了一些算法,也許看上去都很基礎,很簡單。但我相信“千里之行,始於足下”,我也相信,能拿到二等獎,沒有基礎是不可能的。

以下是我積累到的一些算法

1,求最大公約數與最大公倍數:

------------------------------------------------------------

//------------------------------------------輾轉相除法(非遞歸)
   public static int gcd1(int a , int b){    //求最大公約數
	   int temp = 0 ;
	   if(a<b){
		   temp = a ;
		   a = b ;
		   b = temp ; 
	   }
	   
	   while(a%b!=0){
		   temp = b ;
		   b = a%b ;
		   a = temp ;
	   }
	   
	   return b ;
   }
   
   public static int gcd2(int a , int b){   //求最小公倍數
	   int temp = 0 ;
	   int x = a ;
	   int y = b ;
	   if(a<b){
		   temp = a ;
		   a = b ;
		   b = temp ; 
	   }
	   
	   while(a%b!=0){
		   temp = b ;
		   b = a%b ;
		   a = temp ;
	   }
	   
	   return y*x/b ;
   }
}

2,Java中的四捨五入法:

-------------------------------------------------------------------------

package org.jian.acm;

import java.text.DecimalFormat;

public class Main {
	private static DecimalFormat df1 = new DecimalFormat("0.00") ;//直接保留兩位小數
	private static DecimalFormat df2 = new DecimalFormat("#.00") ;//如果整數位爲0,直接去掉
	private static DecimalFormat df3 = new DecimalFormat("0.##") ;//如果保留小數後還有0,則去掉這些0
	
	
    public static void main(String[] args) {
    	double result = 0.70443800 ;
    	System.out.println(result);
		System.out.println(df1.format(result));    
		System.out.println(df2.format(result));    
		System.out.println(df3.format(result));    
	}
}

JDK官方文檔上的DecimalFormat的"0"和"#"的意思。

上面運算結果:

-----------------------------------------------------------------

0.704438
0.70
.70
0.7


3,判斷素數:

----------------------------------------------------------------------------------------------------------------

public static Boolean Prime(int n) {
        for (int i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

4,大數階乘:

-----------------------------------------------------------------------------------------------------------

package org.jian.acm;

import java.math.BigInteger;

public class Main {
	public static void main(String[] args) {
		BigInteger result = new BigInteger("1");// 爲result賦初始值,爲1
		for (int i = 1; i <= 100; i++) {
			BigInteger num = new BigInteger(String.valueOf(i));
			result = result.multiply(num);// 調用自乘方法
		}
		System.out.println(result);
	}
	

}

-----------------------------------------------------------------------------------------------------結果:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941

463976156518286253697920827223758251185210916864000000000000000000000000

5,求出一個數字的各個位數:

package org.jian.acm;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
    	long n = Long.MAX_VALUE ;
    	int a[] = sumDigits(n) ;
    	System.out.println("n:"+n);
    	System.out.println("n的各個位數:"+Arrays.toString(a));
	}
    
    public static int[] sumDigits(long n){
        long temp = n ;
        int num = 0 ;
        while(temp!=0){
         temp/=10 ;
         num++ ;
        }
        int a[] = new int[num] ;
        int sum=0;
        int i = 0 ;
        while(n>0){
         int m=(int)(n%10);
         a[i] = m;
         n=n/10;
         i++ ;
        }
        return a;
       }
}

運算結果:-------------------------------------------------------------------------

n:9223372036854775807
n的各個位數:[7, 0, 8, 5, 7, 7, 4, 5, 8, 6, 3, 0, 2, 7, 3, 3, 2, 2, 9]

6,String切割:

-------------------------------------------------------------------------------------------------------------------

package org.jian.acm;

import java.util.Arrays;


public class Main {
    public static void main(String[] args) {
        String str1 = "asa.tfwerew.qweqd.xcx" ;
        String str2 = "asa tfwerew  qweqd   xcx" ;
        String str3 = "asa tfwerew qweqd xcx" ;
        String str4 = "asa>tfwerew>qweqd>xcx" ;
        
        String strs1[] = str1.split("\\.") ;
        String strs2[] = str2.split("\\s+") ;//+號表示一個或多個的意思
        String strs3[] = str3.split("\\s") ;//\\s表示 空格,回車,換行等空白符,    
        String strs4[] = str4.split("\\>") ;
        
        
        System.out.println(Arrays.toString(strs1));
        System.out.println(Arrays.toString(strs2));
        System.out.println(Arrays.toString(strs3));
        System.out.println(Arrays.toString(strs4));
    }
    
}

輸出結果:---------------------------------

[asa, tfwerew, qweqd, xcx]
[asa, tfwerew, qweqd, xcx]
[asa, tfwerew, qweqd, xcx]
[asa, tfwerew, qweqd, xcx]

~注:

但"a.b.c".split(".");得不到預期的結果: a b c
所以必須要 "a.b.c".split("\\."); 用\\才行 7,JDK本身的進制轉換: ------------------------------------------------------------------
package org.jian.acm;

public class Main {
    public static void main(String[] args) {
    	int a = 200 ;
    	
    	System.out.println("二進制:"+Integer.toBinaryString(a));
    	System.out.println("八進制:"+Integer.toOctalString(a));
    	System.out.println("十六進制:"+Integer.toHexString(a));
    	System.out.println("把進制相反方向轉回來:");
    	System.out.println(Integer.parseInt("11001000", 2));   //11001000二進制轉爲十進制
    	System.out.println(Integer.parseInt("310", 8));        //310八進制轉爲十進制
    	System.out.println(Integer.parseInt("c8", 16));        //C8十六進制轉爲十進制
	}

}

運算結果:------

二進制:11001000
八進制:310
十六進制:c8
把進制相反方向轉回來:
200
200
200


----------------------------------------

BigInteger的進制轉換:

System.out.println(new BigInteger("2131232132132132131221321312")
                .toString(2)); // 化爲二進制

        System.out.println(new BigInteger("FFFFFFFFFFFFFFF", 16));  //十六進制轉二進制

結果:

1101110001011101001111101010110001011101100111110111000000001101100011011001011011001100000
1152921504606846975







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章