【问题标题】:Sum of two really big numbers两个非常大的数字之和
【发布时间】:2016-08-25 14:11:44
【问题描述】:

我的任务是将一个非常大的数字加到另一个数字上并打印结果。 这是我的实现,它应该给出 1000 作为输出,但它写的是 0000。

它应该如何工作: 如果两个数的长度相等: 剩下的数字 %10 只需 /10 。如果结果的长度大于原始长度,则将最后一个余数添加到输出数组的最后一个元素中。

如果两个数字的长度不同: 对相交部分求和,并将余数(以 temp 为单位)添加到相对补码中。

怎么了?

static int[] SumOfBigNumbers(int[] firstNumber, int[] secondNumber)
{
    int temp = 0;
    int maxLength = (Math.Max(firstNumber.Length, secondNumber.Length));
    int minLength = (Math.Min(firstNumber.Length, secondNumber.Length));
    int[] output = new int[maxLength + 1];

    //sum of equal part
    for (int counter = 0; counter < minLength; counter++)
    {
        output[counter] = (firstNumber[counter] + secondNumber[counter] + temp) % 10;
        temp = (firstNumber[counter] + secondNumber[counter] + temp) / 10;
    }
    //exceptions add the temp to the bigger array
    if (temp!=0)
    {
        //if first array is bigger than the second
        if (firstNumber.Length > secondNumber.Length)
        {
            for (int i = minLength; i < maxLength + 1; i++)
            {
                output[i] = (firstNumber[i] + temp) % 10;
                temp = (firstNumber[i] + temp) / 10;
            }
        }
        //if second array is bigger than the first
        else if (firstNumber.Length < secondNumber.Length)
        {
            for (int i = minLength; i < maxLength + 1; i++)
            {
                output[i] = (secondNumber[i] + temp) % 10;
                temp = (secondNumber[i] + temp) / 10;
            }
        }
        //if two number has equal length but there is temp left
        else
        {
            output[maxLength] = temp;
        }
    }
    return output;
}
static void Main()
{
    int[] firstArray = new int[3] { 0, 0, 5 };
    int[] secondArray = new int[3] { 0, 0,5 };
    int[] output = SumOfBigNumbers(firstArray, secondArray);
    foreach (var i in output)
    {
        Console.WriteLine(output[i]);
    }
}

编辑:如果我复制任务会更好:编写一个计算两个非常长的正整数之和的方法。数字表示为数组数字,最后一位数字存储在索引 0 处的数组中。使该方法适用于长度不超过 10,000 位的所有数字。

【问题讨论】:

  • 可能BigInteger 是你正在寻找的东西
  • 也许只有我一个人,但我不明白为什么{0,0,5}+{0,0,5} 会导致{1,0,0,0}。如果它在{5,0,0}+{5,0,0} 的位置对我来说很有意义。
  • 也许我误解了任务。我将其读作第 0 个元素(个)0、第 1 个(十个)元素 0、第 2 个元素(数百个)等等。使用此逻辑,输出应为 0001,稍后可以反转。
  • @Filburt 索引越高,值越显着。

标签: c#


【解决方案1】:

虽然 BigInteger 是处理大数字的更好方法,但我认为您的错误存在于打印输出时。试试这个:

foreach (var i in output)
{
  Console.WriteLine(i);
}

这也将打印0001,最好在打印时反转它

【讨论】:

  • 关于颠倒顺序的公平点。关于你写的代码和我在底部写的不一样吗?
  • 在您的问题中,您试图输出输出的第 i 个元素(输出 [i]),而 i 实际上是数字本身不是索引,尝试将数组中的 5 之一更改为 4 你会看到索引越界异常
【解决方案2】:

您的代码运行良好,但在显示 output 数组时存在一个小错误。

您使用foreach (var i in output) {,但您使用i 作为索引,但事实并非如此。它已经是实际值。只需将i 写入控制台即可。

foreach (var i in output)
{
    Console.WriteLine(i);
}

// Output:
// 0
// 0
// 0
// 1

或使用for 循环按索引遍历数组。

for (int i = 0; i < output.Length; i++)
{
    Console.WriteLine(output[i]);
}

// Output:
// 0
// 0
// 0
// 1

【讨论】:

  • 感谢您的详细解答!现在我明白是什么导致了错误。我认为 foreach 可以像简单的 for() 这样的任何其他循环一样工作。
猜你喜欢
  • 2013-11-21
  • 1970-01-01
  • 2019-12-14
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多