using System.Collections.Generic;
using System.Text;
namespace maxSubsequenceSum_n3
{
//求最大连续子序列之和的立方算法。
//a是待分析数组,试举例。
class Program
{
static void Main(string[] args)
{
int[] a = new int[10]
{14,57,3,1,-58,-67,-9,0,78,444};
int maxSum = 0;
int thisSum = 0;
int a_lengh = a.Length;//获取a的数组长度
for (int i=0;i<a_lengh ;i++ )
{
for (int j=i;j<a_lengh ;j++ )
{
//此时已经获取一个子序列,求其和
thisSum = 0;
for (int k = i; k <= j; k++)
{
thisSum += a[k];
}
if (thisSum > maxSum)
maxSum =thisSum ;
}
}
Console.WriteLine("最大连续子序列之和为:{0}",maxSum );
Console.Read();
}
}
}
本文介绍了一个求最大连续子序列之和的算法实现,通过三层循环遍历数组的所有可能子序列,计算每个子序列的和并更新最大值。最终输出最大连续子序列的和。

2万+

被折叠的 条评论
为什么被折叠?



