1. 程式人生 > >華為機試——兩個超長正整數的加法 java

華為機試——兩個超長正整數的加法 java

題目描述:請設計一個演算法完成兩個超長正整數的加法。 

要求實現函式:

  void AddLongInteger(char * pcAddend, char * pcAugend, char * pcAddResult);

輸入引數:

        char * pcAddend:加數

        char * pcAugend:被加數

        char * pcAddResult:加法結果

返回值:無

執行時間限制: 1 Sec
記憶體限制: 128 MByte
輸入:

兩個超長正整數的字串

輸出:

相加後結果的字串

樣例輸入:
123456789123456789 123456789123456789
樣例輸出:
246913578246913578

  1. import java.util.Arrays;  
  2. import java.util.Scanner;  
  3. publicclass Main {  
  4.     publicstaticvoid main(String[] args) {  
  5.         // TODO Auto-generated method stub
  6.         Scanner sc = new Scanner(System.in);  
  7.         char[] a1 = sc.next().toCharArray();  
  8.         char
    [] a2 = sc.next().toCharArray();  
  9.         char[] result = AddLongInteger(a1,a2);  
  10.         for(int i = 0;i < result.length;i++){  
  11.             System.out.print(result[i]);  
  12.         }         
  13.     }  
  14.     /* 
  15.      * AddLongInteger(char[] a1, char[] a2)的思想: 
  16.      * 首先:a1[0]對應的是第一個被加數的最高位,a2[0]同樣,所以做加法的時候應該從a1[a1.length-1]位開始,或者將a1前後轉置
     
  17.      * 然後加法的計算是:(a1[1]+a2[1]+進位)%10   進位=(a1[1]+a2[1]+進位)/10 
  18.      * 然後就是字元與數字之前的轉換,千萬不要搞混 
  19.      */
  20.     publicstaticchar[] AddLongInteger(char[] a1, char[] a2){  
  21.         int len=0;  
  22.         if(a1.length > a2.length){  
  23.             len = a1.length + 1;  
  24.         }else{  
  25.             len = a2.length + 1;  
  26.         }  
  27.         char[] temp = newchar[len];  
  28.         char[] result;  
  29.         char[] b1 = newchar[a1.length];  
  30.         char[] b2 = newchar[a2.length];  
  31.         for(int i = 0;i < a1.length;i++){  
  32.             b1[a1.length - 1 - i] = a1[i];  
  33.         }                 
  34.         for(int i = 0;i < a2.length;i++){  
  35.             b2[a2.length - 1 - i] = a2[i];  
  36.         }  
  37.         for(int i = 0; i < temp.length; i++){  
  38.             temp[i] = '0';  
  39.         }  
  40.         for(int i = 0; i < len-1;i++){  
  41.             int res = 0;  
  42.             if(b1.length -1 - i >=0 && b2.length -1 - i >= 0){  
  43.                  res = b1[i] - '0' + b2[i] - '0';                             
  44.             }elseif(b1.length -1 - i >= 0 && b2.length -1 - i < 0){  
  45.                  res = b1[i] - '0';                   
  46.             }elseif(b1.length -1 - i < 0 && b2.length -1 - i >= 0){  
  47.                  res = b2[i] - '0';  
  48.             }  
  49.             int value = temp[i]-'0'+res;  
  50.             temp[i] = (char) (value%10 + '0') ;               
  51.             temp[i+1] += value/10;  
  52. //          System.out.println(temp[i]);
  53. //          System.out.println(temp[i+1]);
  54.         }  
  55.         while(temp[len - 1] == '0'){  
  56.             len--;  
  57.         }     
  58.         result = newchar[len];  
  59.         int s = result.length;  
  60.         for(int i = 0;i < s; i++){  
  61.             result[s-1-i] = temp[i];  
  62.         }  
  63.         return result;  
  64.     }  
  65. }