如何从数组中的任意两个元素中找到最大可能的总和
How to find the maximum possible sum from any two elements in an array
数组元素为
int[] a = {7,5,8,9,-10,34,-150,60}
求上述数组中任意两个元素的最大可能总和的代码是什么?
您可以使用 Linq :
a.OrderByDescending(z=>z).Take(2).Sum()
我试过下面的方法,效果很好
int[] a = new int[10] { 10, 34, 56, -150, -200, 49, 63, 108, -94, -84 };
int[] result = a.OrderByDescending(x => x).ToArray();
Console.WriteLine(result[0]+" + "+result[1]+" = "+(int)(result[0]+result[1]));
108 + 63 = 171
按任意键继续 。 . .
数组元素为
int[] a = {7,5,8,9,-10,34,-150,60}
求上述数组中任意两个元素的最大可能总和的代码是什么?
您可以使用 Linq :
a.OrderByDescending(z=>z).Take(2).Sum()
我试过下面的方法,效果很好
int[] a = new int[10] { 10, 34, 56, -150, -200, 49, 63, 108, -94, -84 };
int[] result = a.OrderByDescending(x => x).ToArray();
Console.WriteLine(result[0]+" + "+result[1]+" = "+(int)(result[0]+result[1]));
108 + 63 = 171 按任意键继续 。 . .