大整數處理

處理兩大數相乘,以下爲Java代碼:

  1. public class BigInt { 
  2.  
  3.     /** 
  4.      * @param args 
  5.      */ 
  6.     public static void main(String[] args) { 
  7.         String num1 = "1234" ; 
  8.         String num2 = "56789" ; 
  9.          
  10.         mult(num1, num2) ; 
  11.     } 
  12.      
  13.     /** 
  14.      * 直接輸出任意兩數相乘 
  15.      * @param n1 
  16.      * @param n2 
  17.      */ 
  18.     public static void mult(String n1, String n2) { 
  19.         int[] result = new int[n1.length()+n2.length()] ; // 這裏爲存放兩數相乘後的得數。這個可以動態規劃 
  20.          
  21.         // 字符串轉數字數組處理,算法的效率就是在這些地方浪費的 
  22.         int[] num1 = new int[n1.length()] ; 
  23.         int[] num2 = new int[n2.length()] ; 
  24.         for(int i = 0; i < n1.length(); i++) { 
  25.             num1[i] = Character.getNumericValue(n1.charAt(i)) ; 
  26.         } 
  27.         for(int i = 0; i < n2.length(); i++) { 
  28.             num2[i] = Character.getNumericValue(n2.charAt(i)) ; 
  29.         } 
  30.          
  31.         // 核心代碼,其實簡單來說就是我們筆算怎麼算,代碼就怎麼算,這樣無論多大的數,都是可以算的 
  32.         // 裏面稍微做了些細緻的處理,認真看很容易就能明白 
  33.         int tempNum ; 
  34.         int tt ; 
  35.         for(int i = num1.length-1; i >= 0; i--) { 
  36.             for(int j = num2.length-1; j >= 0; j--) { 
  37.                 tempNum = num1[i] * num2[j] ; 
  38.                 tt = result.length - (num1.length-i) - (num2.length-j) ; 
  39.                 if(tempNum >= 10) { 
  40.                     f(result, tt, tempNum % 10) ; 
  41.                     f(result, tt-1, tempNum / 10) ; 
  42.                 }else { 
  43.                     f(result, tt, tempNum) ; 
  44.                 } 
  45.                  
  46.             } 
  47.         } 
  48.  
  49.         // 輸出,這僅僅只是將數組前面的0去掉,去實際的答案 
  50.         boolean found = false ; 
  51.         for(int t : result) { 
  52.             if(t != 0 && found == false) { 
  53.                 System.out.print(t) ; 
  54.                 found = true ; 
  55.             }else if(found == true) { 
  56.                 System.out.print(t) ; 
  57.             } 
  58.         } 
  59.         System.out.println() ; 
  60.          
  61.     } 
  62.      
  63.     /** 
  64.      * 相乘後相加處理,有關於兩數相加後>=10的時候應該怎麼進一位的處理, 
  65.      * 知道不在有進一位爲止 
  66.      * @param result 
  67.      * @param tTe 
  68.      * @param tempNum1 
  69.      */ 
  70.     public static void f(int[] result, int tTe, int tempNum1) { 
  71.         int t ; 
  72.         while(true) { 
  73.             if(result[tTe+1] + tempNum1 >= 10) { 
  74.                 t = result[tTe+1] ; 
  75.                 result[tTe+1] = (result[tTe+1] + tempNum1) % 10 ;  
  76.                 tTe-- ; 
  77.                 tempNum1 = (t + tempNum1) / 10 ; 
  78.             }else { 
  79.                 result[tTe+1] += tempNum1 ; 
  80.                 break ; 
  81.             } 
  82.         } 
  83.     } 
  84.  


雖說類庫裏面有大數處理類,但爲了提高算法水平,還是自己寫了下,希望對大家學習有幫助。
 

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