在数组中的递减元素序列中找到最高和
Find the highest sum among the decreasing sequence of elements in an array
例如我们有一个数组
{9,8,7,9,5,4,10,3,12}
现在递减序列是
9,8,7 => 9+8+7 = 24
9,5,4 => 9+5+4 = 18
10,3 => 10+3 =13
12 => 12
在上面的求和中,24 是最高的,我们必须打印该值作为结果。
不应使用数组列表等内置函数
需要一个动态答案并且答案必须满足该类型的所有其他数组
我使用的代码是
int[] arr2 = new int[8] { 10, 9, 8, 9, 7, 6, 11, 5 };
int temp2 = 0;
for (int i = 1; i <= arr2.Length - 1; i++)
{
for (int j = i - 1; j <= arr2.Length - 1; j++)
{
if (arr2[j] > arr2[i])
{
temp2 = temp2 + arr2[i]+arr2[j];
}
}
}
我还没有对此进行测试,但我认为它会让您大致了解如何进行。
public int FindMaxDecreasing(int[] arr)
{
//Store the max value
var max = 0;
//The current running total
var temp = 0;
//Store the previous value
var previous = 0;
for(int i = 0; i < arr.Length; i++)
{
//Check if first element or decreasing value
if(i == 0 += arr[i] < previous)
{
//Add to temp if it is
temp += arr[i];
}
else
{
//Swap out max value if temp is larger
if(temp > max)
{
max = temp;
}
//Restart temp
temp = arr[i];
}
//Assign previous
previous = arr[i];
}
if(temp > max)
{
max = temp;
}
return max;
}
基本,未优化:
int[] arr = { 9, 8, 7, 9, 5, 4, 10, 3, 12 };
int maxSum = 0;
int curSum = 0;
for(int i = 0; i < arr.Length; i++)
{
// new sequence
if(curSum == 0)
{
curSum = arr[i];
}
// seqence decreasing
else if(arr[i] <= arr[i - 1])
{
curSum += arr[i];
}
// end of sequence
else
{
// check if the sequence produced a greater sum
if(maxSum < curSum)
{
maxSum = curSum;
}
Console.WriteLine(curSum);
curSum = arr[i];
}
}
Console.WriteLine(curSum);
// final check
if(curSum > maxSum)
{
maxSum = curSum;
}
Console.WriteLine($"Max: {maxSum}");
例如我们有一个数组
{9,8,7,9,5,4,10,3,12}
现在递减序列是
9,8,7 => 9+8+7 = 24
9,5,4 => 9+5+4 = 18
10,3 => 10+3 =13
12 => 12
在上面的求和中,24 是最高的,我们必须打印该值作为结果。
不应使用数组列表等内置函数
需要一个动态答案并且答案必须满足该类型的所有其他数组
我使用的代码是
int[] arr2 = new int[8] { 10, 9, 8, 9, 7, 6, 11, 5 };
int temp2 = 0;
for (int i = 1; i <= arr2.Length - 1; i++)
{
for (int j = i - 1; j <= arr2.Length - 1; j++)
{
if (arr2[j] > arr2[i])
{
temp2 = temp2 + arr2[i]+arr2[j];
}
}
}
我还没有对此进行测试,但我认为它会让您大致了解如何进行。
public int FindMaxDecreasing(int[] arr)
{
//Store the max value
var max = 0;
//The current running total
var temp = 0;
//Store the previous value
var previous = 0;
for(int i = 0; i < arr.Length; i++)
{
//Check if first element or decreasing value
if(i == 0 += arr[i] < previous)
{
//Add to temp if it is
temp += arr[i];
}
else
{
//Swap out max value if temp is larger
if(temp > max)
{
max = temp;
}
//Restart temp
temp = arr[i];
}
//Assign previous
previous = arr[i];
}
if(temp > max)
{
max = temp;
}
return max;
}
基本,未优化:
int[] arr = { 9, 8, 7, 9, 5, 4, 10, 3, 12 };
int maxSum = 0;
int curSum = 0;
for(int i = 0; i < arr.Length; i++)
{
// new sequence
if(curSum == 0)
{
curSum = arr[i];
}
// seqence decreasing
else if(arr[i] <= arr[i - 1])
{
curSum += arr[i];
}
// end of sequence
else
{
// check if the sequence produced a greater sum
if(maxSum < curSum)
{
maxSum = curSum;
}
Console.WriteLine(curSum);
curSum = arr[i];
}
}
Console.WriteLine(curSum);
// final check
if(curSum > maxSum)
{
maxSum = curSum;
}
Console.WriteLine($"Max: {maxSum}");