给定K个整数组成的序列{ N1, N2, …, NK },“连续子列”被定义为{ Ni, Ni+1, …, Nj },其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。
要求编写程序,计算给定整数序列的最大子列和。本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下:
输入格式:
输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。
输出格式:
在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。
输入样例:
6
-2 11 -4 13 -5 -2
输出样例:
20
总共有四种处理方法,有需要的可以参考,自己已经测试过没有问题,代码如下:
//==========================================
// Filename : 最大子列和问题
// Time : 2019年5月23日
// Author : 柚子树
// Email : [email protected]
//==========================================
#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
int MaxSubseqSum(int arr[], int length);
int main()
{
int arr[6] = { -2, 11, -4, 13, -5, -2 };
int length = 6;
int MaxSum = MaxSubseqSum(arr, length);
cout << "MaxSubseqSum = " << MaxSum << endl;
system("pause");
return EXIT_SUCCESS;
}
int MaxSubseqSum(int arr[], int length)
{
int MaxSum = 0;
for (size_t i = 0; i < length; i++)
{
for (size_t j = i; j < length; j++)
{
int ThisSum = 0;
for (size_t k = i; k <= j; ++k)
{
ThisSum += arr[k];
}
if (ThisSum > MaxSum)
{
MaxSum = ThisSum;
}
}
}
return MaxSum;
}
int MaxSubseqSum(int arr[], int length)
{
int MaxSum = 0;
for (size_t i = 0; i < length; i++)
{
int ThisSum = 0;
for (size_t j = i; j < length; j++)
{
ThisSum += arr[j];
if (ThisSum > MaxSum)
{
MaxSum = ThisSum;
}
}
}
return MaxSum;
}
在写这个算法的时候,第一次调试报错栈溢出,打断点调试也没发现原因,后来查阅网上的资料比对代码将 循环变量 i
的类型由 size_t
类型改为 int
类型,就没有问题了,暂时不清楚这个问题,有知道的希望不吝赐教。
int MaxSubseqSum(int arr[], int left, int right)
{
//递归结束条件
if (left == right)
{
return arr[left] > 0 ? arr[left] : 0;
}
//左右最大子列和
int mid = (left + right) >> 1;
int LeftMaxSum = MaxSubseqSum(arr, left, mid);
int RightMaxSum = MaxSubseqSum(arr, mid + 1, right);
//跨分界线最大子列和
int LeftBorderMaxSum = 0;
int LeftBorderSum = 0;
for (int i = mid; i >= left; i--)
{
LeftBorderSum += arr[i];
if (LeftBorderSum > LeftBorderMaxSum)
{
LeftBorderMaxSum = LeftBorderSum;
}
}
int RightBorderMaxSum = 0;
int RightBorderSum = 0;
for (int i = mid + 1; i <= right; i++)
{
RightBorderSum += arr[i];
if (RightBorderSum > RightBorderMaxSum)
{
RightBorderMaxSum = RightBorderSum;
}
}
int BorderMaxSum = LeftBorderMaxSum + RightBorderMaxSum;
int LRMaxSum = LeftMaxSum > RightMaxSum ? LeftMaxSum : RightMaxSum;
return LRMaxSum > BorderMaxSum ? LRMaxSum : BorderMaxSum;
}
int MaxSubseqSum(int arr[], int length)
{
int MaxSum = 0;
int ThisSum = 0;
for (size_t i = 0; i < length; i++)
{
ThisSum += arr[i];
if (ThisSum > MaxSum)
{
MaxSum = ThisSum;
}
else if (ThisSum < 0)
{
ThisSum = 0;
}
}
return MaxSum;
}
提交代码:
#include
using namespace std;
int MaxSubseqSum(int arr[], int length)
{
int MaxSum = 0;
int ThisSum = 0;
for (int i = 0; i < length; i++)
{
ThisSum += arr[i];
if (ThisSum > MaxSum)
{
MaxSum = ThisSum;
}
else if (ThisSum < 0)
{
ThisSum = 0;
}
}
return MaxSum;
}
int arr[100000] = { 0 };
int main()
{
int length;
cin >> length;
for(int i = 0; i < length; ++i)
{
cin >> arr[i];
}
int MaxSum = MaxSubseqSum(arr, length);
cout << MaxSum << endl;
return 0;
}