//分治算法
#include <stdio.h>
int MaxSubSum(int *a, int left, int right);
int MaxSum(int n, int *a);
int main()
{
int a[] = { -2, 11, -4, 13, -5, -2 };
for (int i = 0; i<6; i++)
{
printf("%d ", a[i]);
}
printf("\n");
printf("数组a的最大连续子段和为:%d\n", MaxSum(6, a));
return 0;
}
int MaxSubSum(int *a, int left, int right)
{
int sum = 0;
if (left == right)
{
sum = a[left]>0 ? a[left] : 0;
}
else
{
int center = (left + right) / 2;
int leftsum = MaxSubSum(a, left, center);
int rightsum = MaxSubSum(a, center + 1, right);
int s1 = 0;
int lefts = 0;
for (int i = center; i >= left; i--)
{
lefts += a[i];
if (lefts>s1)
{
s1 = lefts;
}
}
int s2 = 0;
int rights = 0;
for (int i = center + 1; i <= right; i++)
{
rights += a[i];
if (rights>s2)
{
s2 = rights;
}
}
sum = s1 + s2;
if (sum<leftsum)
{
sum = leftsum;
}
if (sum<rightsum)
{
sum = rightsum;
}
}
return sum;
}
int MaxSum(int n, int *a)
{
return MaxSubSum(a, 0, n - 1);
}
枫蜜柚子茶
- 粉丝: 9018
- 资源: 5350
最新资源
- 1Cr18Ni9Ti钢板焊接接头冷弯开裂的试验研究.pdf
- 1Cr18Ni9Ti钎焊接头组织和性能影响研究 - .pdf
- 2.25 Cr-1 Mo钢制加氢反应器的焊接裂纹(二)-对焊接裂纹性质的试验研究.pdf
- 1Crl8Ni9Ti不锈钢复合管系的焊接 - .pdf
- 2.25Cr-1Mo-0.25V低合金钢用焊接材料的检验 - .pdf
- 2.25-1Mo钢焊后产生焊接裂纹的原因分析及其返修措施.pdf
- 2.25 Cr-1 Mo钢制加氢反应器的焊接裂纹(一)-在制过程中出现的焊接裂纹.pdf
- 2.25Cr-1Mo-0.25V钢焊接材料的再热裂纹敏感性试验方法探讨.pdf
- 2A12厚板铝合金搅拌摩擦焊接头组织性能研究 - .pdf
- 2万立立式液氢高压容器的焊接研制.pdf
- 2A14铝合金法兰焊接裂纹修复.pdf
- 3.5%Ni钢焊接接头的低温韧性研究三──熔敷金属碳含量等因素的影响.pdf
- 3.5%Ni钢焊接技术的最近进展.pdf
- 3.5%Ni钢焊接接头低温韧性的研究二──焊后热处理母材及焊材的影响.pdf
- 3.5%Ni钢焊接接头低温韧性的研究一──线能量层间温度的影响.pdf
- 3.5Ni钢低温设备的制造和焊接.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈