简体   繁体   English

C# 中 integer 数组的所有值求和期间溢出异常

[英]Overflow exception during sum of all the values of integer array in C#

Hi I was solving a very basic problem on Hackerrank about determining Mini-Max Sum of all the values in an integer array.嗨,我在 Hackerrank 上解决了一个非常基本的问题,即确定 integer 数组中所有值的最小最大总和。 Basically given an array of 5 values like [1, 2, 3, 4, 5], the minimum sum will be 1+2+3+4 = 10 and maximum sum will be 2+3+4+5 = 14. Very simple question, but I am running into an issue with a certain large input values.基本上给定一个包含 5 个值的数组,例如 [1, 2, 3, 4, 5],最小和将为 1+2+3+4 = 10,最大和为 2+3+4+5 = 14。非常一个简单的问题,但我遇到了某个大输入值的问题。 Below are my solutions and their result.以下是我的解决方案及其结果。

//Input: 256741038 623958417 467905213 714532089 938071625
//Output: 2063136757 2744467344

Below is using inbuilt Sum() method.下面是使用内置的Sum()方法。

static void Main(string[] args)
{
    int[] arr = new int[5] {256741038,623958417,467905213,714532089,938071625};
    Array.Sort(arr);
    long arrSum = arr.Sum();  //System.OverflowException: 'Arithmetic operation resulted in an overflow.'
    long minSum = arrSum - arr[arr.Length - 1];
    long maxSum = arrSum - arr[0];
    Console.WriteLine(minSum + " " + maxSum);
}

Below is using Aggregate extension method.下面是使用聚合扩展方法。

static void Main(string[] args)

{
    int[] arr = new int[5] {256741038,623958417,467905213,714532089,938071625};
    Array.Sort(arr);
    long arrSum = arr.Aggregate((total, next) => total + next); // arrSum = -1293758914
    long minSum = arrSum - arr[arr.Length - 1];
    long maxSum = arrSum - arr[0];
    Console.WriteLine(minSum + " " + maxSum);
}

Output: -2231830539 -1550499952

And if I use the regular foreach loop like below:如果我使用如下的常规 foreach 循环:

static void miniMaxSum(int[] arr)
{
    Array.Sort(arr);
    long arrSum = 0;
    foreach (var value in arr)
    {
        arrSum += value;
    }
    long minSum = arrSum - arr[arr.Length - 1];
    long maxSum = arrSum - arr[0];
    Console.WriteLine(minSum + " " + maxSum);
}

The output is correct as expected i.e. 2063136757 2744467344

The OverflowException class description here says "An arithmetic operation produces a result that is outside the range of the data type returned by the operation". 此处的 OverflowException class 描述说“算术运算产生的结果超出了操作返回的数据类型的范围”。 But the arrSum value is within long range so I am not able to figure out the issue.但是arrSum值在long范围内,所以我无法找出问题所在。 So not sure if I am missing anything or using the functions incorrectly but not able to understand this behavior.所以不确定我是否遗漏了任何东西或错误地使用了这些功能,但无法理解这种行为。

Any detail is appreciated.任何细节表示赞赏。

Reason its failing is because the Sum() method is being executed on the int arrays.它失败的原因是因为 Sum() 方法正在int arrays 上执行。 If you convert the numbers of the int array to long and then run the Sum operation on them, then you will get your result.如果您将 int 数组的数字转换为 long,然后对它们运行 Sum 操作,那么您将得到结果。

long arrSum = nums.Sum(x => (long)x);

// or, for readability
long arrSum = nums.Select(x => (long)x).Sum();

This will run the Sum operation only on long s.这将仅在long s 上运行 Sum 操作。 Way you are doing it, it sums up the integers until it is done with all of them and "then" saves it to your long.. while doing the addition, it fails because Int32 cant go too big.你这样做的方式,它总结了整数,直到它完成所有整数,然后“然后”将它保存到你的 long.. 在进行加法时,它失败了,因为 Int32 不能 go 太大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM