基礎知識
對於二進制來說,最高位代表正負號,-0表示-128,+0表示0
32位系統int型4個字節:-(2的31次方) ~ (2的31次方) 減 1
最大負數:10000000 00000000 00000000 00000000
最大正數:01111111 11111111 11111111 11111111
0: 00000000 00000000 00000000 00000000
64位系統同理,int型表示范圍是:-(2的63次方) ~ (2的63次方) 減 1
具體內容
大數操作
正常情況下一個整數最多只能放在long類型之中,但是如果現在有如下的一個數字:
1111111111111111111111111111111111111111111111111
根本就是無法保存的,所以為了解決這樣的問題,在java中引入了兩個大數的操作類:
操作整型:BigInteger
操作小數:BigDecimal
當然了,這些大數都會以字符串的形式傳入。
BigInteger
如果在操作的時候一個整型數據已經超過了整數的最大類型長度long的話,則此數據就無法裝入,所以,此時要使用BigInteger類進行操作。
BigInteger是在java.math包中。
代碼示例:
package ustc.lichunchun.bigdataapi; import java.math.BigInteger; public class BigIntegerDemo1 { public static void main(String[] args) { BigInteger bi1 = new BigInteger("123456789") ; // 聲明BigInteger對象 BigInteger bi2 = new BigInteger("987654321") ; // 聲明BigInteger對象 System.out.println("加法操作:" + bi2.add(bi1)) ; // 加法操作 System.out.println("減法操作:" + bi2.subtract(bi1)) ; // 減法操作 System.out.println("乘法操作:" + bi2.multiply(bi1)) ; // 乘法操作 System.out.println("除法操作:" + bi2.divide(bi1)) ; // 除法操作 System.out.println("最大數:" + bi2.max(bi1)) ; // 求出最大數 System.out.println("最小數:" + bi2.min(bi1)) ; // 求出最小數 BigInteger result[] = bi2.divideAndRemainder(bi1) ; // 求出余數的除法操作 System.out.println("商是:" + result[0] + ";余數是:" + result[1]) ; } }
發現divide()方法本身只是把最終的商保存下來了,但是這樣的兩個數字相除的時候肯定是無法整除,肯定存在余數,所以我們在上面代碼中還用到了divideAndRemainder()方法來獲得結果和余數。
BigDecimal
使用此類可以完成大的小數操作,而且也可以使用此類進行精確的四舍五入,這一點在開發中經常使用。
對於不需要任何准確計算精度的程序可以直接使用float或double完成,但是如果需要精確計算結果,則必須使用BigDecimal類。
代碼示例:
package ustc.lichunchun.bigdataapi; import java.math.BigDecimal; public class BigDecimalDemo01 { public static void main(String[] args) { System.out.println("加法運算:" + MyMath.round(MyMath.add(10.345,3.333),1)) ; System.out.println("減法運算:" + MyMath.round(MyMath.sub(10.345,3.333),3)) ; System.out.println("乘法運算:" + MyMath.round(MyMath.mul(10.345,3.333),4)) ; System.out.println("除法運算:" + MyMath.div(10.345,3.333,3)) ; } } class MyMath{ public static double add(double d1,double d2){ // 進行加法計算 BigDecimal b1 = new BigDecimal(d1) ; BigDecimal b2 = new BigDecimal(d2) ; return b1.add(b2).doubleValue() ; } public static double sub(double d1,double d2){ // 進行減法計算 BigDecimal b1 = new BigDecimal(d1) ; BigDecimal b2 = new BigDecimal(d2) ; return b1.subtract(b2).doubleValue() ; } public static double mul(double d1,double d2){ // 進行乘法計算 BigDecimal b1 = new BigDecimal(d1) ; BigDecimal b2 = new BigDecimal(d2) ; return b1.multiply(b2).doubleValue() ; } public static double div(double d1,double d2,int len){ // 進行除法計算 BigDecimal b1 = new BigDecimal(d1) ; BigDecimal b2 = new BigDecimal(d2) ; return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ; } public static double round(double d,int len){ // 進行四舍五入 BigDecimal b1 = new BigDecimal(d) ; BigDecimal b2 = new BigDecimal(1) ; // 技巧 return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ; } };
華為機試題
現在我們來看一道華為的機試題:
寫出一個程序,接受一個十六進制的數值字符串,輸出該數值的十進制字符串。(多組同時輸入 )
一開始,我寫的答案是這樣的:
package huawei.job; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main5 { public static void main(String[] args) { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); String line ; BigInteger base = new BigInteger("16"); try { while((line = bufr.readLine()) != null){ line = line.substring(2); int result = Integer.parseInt(line, 16); System.out.println(result); } } catch (IOException e) { e.printStackTrace(); } } }
這里,我直接使用的JavaAPI--Integer提供的方法,將十六進制進行轉換,答案也是牛客網AC。但是,仔細一想,華為這樣的大公司不可能出這么簡單的題目,所以我就想到了處理大數的情況,更改后的代碼如下,同樣牛客AC。
package huawei.job; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main5 { public static void main(String[] args) { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); String line ; BigInteger base = new BigInteger("16"); try { while((line = bufr.readLine()) != null){ line = line.substring(2); //int result = Integer.parseInt(line, 16); BigInteger result = new BigInteger("0"); for(int i = 0; i < line.length(); i++){ char ch = line.charAt(line.length()-1-i); if(ch >= 'A' && ch <= 'F'){ BigInteger tmp = base.pow(i).multiply(new BigInteger(Integer.toString((ch - 'A' + 10)))); result = result.add(tmp); }else{ BigInteger tmp = base.pow(i).multiply(new BigInteger(Character.toString(ch))); result = result.add(tmp); } } System.out.println(result); } } catch (IOException e) { e.printStackTrace(); } } }
總結
1、雖然在開發中很少遇到大數字的操作情況。
2、使用BigDecimal可以指定好四舍五入的精確位置。