【问题标题】:How to add two int array values together?如何将两个 int 数组值相加?
【发布时间】:2016-06-10 22:57:27
【问题描述】:

我正在尝试将两个 int 数组相加以获得总和。第一个数组包含 0000000000000000123456789,第二个数组包含 0001111111111111111111111。总和应该是 1111111111111234567900。另外我正在尝试不使用 BigInteger 或 BigDecimal。

for(int i=0; i<number1.length;i++){
   add= number1[i]+number2[i];
   if(plus>=10){    
       sum[i-1]+=add/10;
       sum[i] = add%10;
    }else{
        sum[i]=add;
    }                   
}

此时产生的输出是 00011111111111112345678100。如何修复我的代码以使 8 变为 9?

【问题讨论】:

  • 什么是plus
  • 这不是我们表示数组的方式......
  • 0000000000000000123456789 中有多少个元素?
  • plus 是一个 int,总共有 25 个元素

标签: java arrays for-loop


【解决方案1】:

这有点工作。我可以想到一些这样的事情会中断的情况,比如如果数组像 {9,9,9} 和 {9,9,9},result 将改为 {9,9,8} {1,9,9,8}。 这是一个小修复,留给读者作为活动。

public static void main(String []args){
    int[] number1 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9};
    int[] number2 = {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

    int carry = 0, sum = 0;

    int[] result = new int[number1.length];
    for (int i = number1.length - 1; i >= 0 ; i--) {
        sum = number1[i] + number2[i] + carry;
        result[i] = sum%10;
        carry = sum/10;
    }
    // [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 9, 0, 0]                                                                   
    System.out.println(Arrays.toString(result)); 
 }

【讨论】:

    【解决方案2】:

    试试这个

            int[] number = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9};
            int[] number2 = {0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
            int[] result = new int[number.length+1];
            int carry = 0;
            for (int i = number.length - 1; i >= 0; i--) {
                result[i+1] = number[i] + number2[i] + carry;
                if (result[i+1] > 9) {
                    carry = result[i+1] - 9;
                    result[i+1] -= 10;
                }
                else {
                    carry = 0;
                }
            }
            System.out.println(Arrays.toString(result));
    

    【讨论】:

      【解决方案3】:

      此代码应该适用于正确地将大于 10 的数字带到下一个并保留较小的数字!

      int[] result = new int[number1.length+1];
              for (int i=number1.length-1; i>=0;i--){
                  result[i+1] = number1[i] + number[i] + result[i+1];
                  if(result[i+1] >= 10){
                      result[i+1] -= 10; // it could never be more than 20 so this is ok and if it is 10 than it will still carry over to the next
                      result[i] = 1; // the ten carried to the next one
                  }
              }
      

      类似于 Seek Addo 的答案,但避免了在他的代码中发现的阻止应用程序正确携带数字的错误。

      这是我能想到的最简洁的方式,并且应该产生正确的答案

      [1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,9,0,0] 或 1111111111111234567900

      【讨论】:

      • result[i] - 10;?
      • int[] result。无法检查 null。
      • 请在提交前编译您的代码。 [web-based sandbox]。这段代码有很多编译错误。
      • 您应该尝试编译并运行您的代码。我以前的 cmets 在您的代码中显示错误
      • for-loop 来自 0 to number1.length-1。 len(结果) = len(number1)。 result[i+1] 行将抛出 ArrayIndexOutOfBoundsException
      【解决方案4】:

      接受的答案不好。这是我的版本(支持不同的数组大小):

      public static int[] sumIntArrays(int[] n1, int[] n2) {
          int n1Size = n1.length - 1, n2Size = n2.length - 1, indexSum = n1Size > n2Size ? n1Size + 2 : n2Size + 2, slack = 0, s;
          int[] sum = new int[indexSum];
          while (true) {
              if (n1Size < 0 && n2Size < 0)
                  break;
              s = (n1Size < 0 ? 0 : n1[n1Size--]) + (n2Size < 0 ? 0 : n2[n2Size--]) + slack;
              if (s > 9) {
                  sum[--indexSum] = s % 10;
                  slack = s / 10;
              } else {
                  sum[--indexSum] = s;
                  slack = 0;
              }
          }
          if (slack != 0)
              sum[0] = slack;
          return sum;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-01
        • 2017-02-26
        • 1970-01-01
        • 2018-04-10
        • 1970-01-01
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多