2015-10-18 19 views
2

我想實現一個像BigInteger類,其中輸入是一個瘋狂的長數字的添加函數。但是,當我從字節數組轉換爲字符串時,屏幕上沒有打印任何內容。但是我的示例代碼在主函數中被註釋掉了。有誰知道爲什麼?java biginteger除了使用字節[]

這是我的上coderpad
resut字節數組輸出:[7,9,1,0]
100 + 9 =

import java.io.*; 
import java.util.*; 
import java.nio.charset.Charset; 

class Solution { 
    private static Charset charset = Charset.forName("UTF-8"); 

    public static class BigInteger { 
    public byte[] digits; 

    public BigInteger(String input) { 
     digits = input.getBytes(charset); 
    } 
    public BigInteger(byte[] digits) { 
     this.digits = digits; 
//   System.out.println(Arrays.toString(digits)); 
    } 

    public String add(BigInteger other) { 
     byte[] digits1, digits2; 
     if (this.digits.length >= other.digits.length) { 
      digits1 = this.digits; 
      digits2 = other.digits; 
     } else { 
      digits1 = other.digits; 
      digits2 = this.digits; 
     } 
     int remainder = 0; 
     int s1 = digits1.length - 1; 
     int s2 = digits2.length - 1; 
     byte[] resultDigits = new byte[s1 + 2]; 

     for (int i = s1; i >= 0; i--) { 
      int digit1 = digits1[i]; 
      int digit2 = i > s2 ? 0 : digits2[i]; 
      int r = digit1 + digit2 + remainder; 
      remainder = r > 9 ? 1 : 0; 
      resultDigits[i] = (byte) (r % 10); 
//    System.out.println(resultDigits[i]); 
     } 
     resultDigits[s1] = (byte) remainder; 

//   System.out.println("resut byte array: " + Arrays.toString(resultDigits)); 
    String result = new String(resultDigits, charset); 
     return result; 
    } 

    } 
    public static void main(String[] args) { 
    BigInteger a = new BigInteger("100"); 
    BigInteger b = new BigInteger("9"); 
    System.out.println("100 + 9 = " + a.add(b)); 

//  String abc="123"; 

//  byte[] ba = abc.getBytes(); 
//  System.out.println(Arrays.toString(ba)); 
//  System.out.println(new String(ba, Charset.forName("UTF-8"))); 
    } 
} 

---編輯 - 的add()V2

public String add(BigInteger other) { 
     byte[] digits1, digits2; 
     if (this.digits.length >= other.digits.length) { 
      digits1 = this.digits; 
      digits2 = other.digits; 
     } else { 
      digits1 = other.digits; 
      digits2 = this.digits; 
     } 
     int remainder = 0; 
     int s1 = digits1.length - 1; 
     int s2 = digits2.length - 1; 
     byte[] resultDigits = new byte[s1 + 2]; 

     for (int i = s1; i >= 0; i--) { 
      int digit1 = digits1[s1--]; 
      int digit2 = s2 >= 0 ? digits2[s2--] : 0; 
      int sum = digit1 + digit2 + remainder; 
      remainder = sum/10; 
      resultDigits[i] = (byte) (sum % 10); 
//    System.out.println(resultDigits[i]); 
     } 
     resultDigits[0] = (byte) remainder; // probably dont need this 

     System.out.println("resut byte array: " + Arrays.toString(resultDigits)); 
    String result = new String(resultDigits, charset); 
    System.out.println("result as string: " + result); 
     return result; 
    } 
+0

不'BigInteger'有'add'方法?你可能無法正確覆蓋它。 – 3kings

+0

我正在實施我自己的;這是我的一個面試問題 – Raptrex

+0

我在我的字符串上調用了isEmpty(),它返回false。我檢查了它的長度,它是4.所以我不知道爲什麼它的4個空格 – Raptrex

回答

1

這裏是一個校正:

public String add(BigInteger other) { 
      byte[] digits1, digits2; 
      if (this.digits.length >= other.digits.length) { 
       digits1 = this.digits; 
       digits2 = other.digits; 
      } else { 
       digits1 = other.digits; 
       digits2 = this.digits; 
      } 
      int remainder = 0; 
      int s1 = digits1.length - 1; 
      int s2 = digits2.length - 1; 
      byte[] resultDigits = new byte[s1 + 2]; 

      for (int i = s1; i >= 0; i--) { 
       int diff = digits1.length - digits2.length; 
       int digit1 = digits1[i] - 48; 
       int digit2 = i < diff ? 0 : digits2[i - diff] - 48; 
       int r = digit1 + digit2 + remainder; 
       remainder = r > 9 ? 1 : 0; 
       resultDigits[i+1] = (byte)(r % 10 + 48); 
      } 
      resultDigits[0] += (byte) remainder + 48; 
      String result = resultDigits[0] == 48 ? new String(resultDigits).substring(1) : new String(resultDigits); 
      return result; 
     } 

基本上,錯誤是:

  • 你不把48你的價值的認識,你都拿到字符輸入
  • 你也有你的小操作數的位置的麻煩。
  • 最後,你在最後加入的剩餘部分被添加在一個不好的位置。您應該始終將其添加到您的第一個索引。

輸出:

0 + 45 = 45 
19 + 95 = 114 
38 + 20 = 58 
57 + 92 = 149 
76 + 67 = 143 
95 + 54 = 149 
114 + 90 = 204 
133 + 48 = 181 
152 + 6 = 158 
171 + 18 = 189 
190 + 22 = 212 
209 + 13 = 222 
228 + 32 = 260 
247 + 24 = 271