首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

这个求最大子序列的程序为什么不能输出正确答案?该怎么解决

2012-02-07 
这个求最大子序列的程序为什么不能输出正确答案??C/C++ code#include stdio.h#include conio.h#define

这个求最大子序列的程序为什么不能输出正确答案??

C/C++ code
#include <stdio.h>#include <conio.h>#define N 6main(){    int i;    int a[N]={-2,11,-4,13,-5,-2};    printf("the old older is:\n");        for(i=0;i<N;i++)            printf("%d  ",a[i]);                printf("\n");                     i=MaxSubsequenceSum4(a,N);                 printf("the max is %d\n",i);                printf("\n");        getch();}intMaxSubsequenceSum4(const int a[],int n)     {    int ThisSum,MaxSum,j;     ThisSum=MaxSum=0;      for(j=0;j<n;j++)      {        ThisSum+=a[j];         if(ThisSum>MaxSum)            MaxSum=ThisSum;         else if(ThisSum<0)            MaxSum=0;      }     return MaxSum;}


[解决办法]
利用动态规划的思想 添加一个数组 记录前n项的和 最大的子串和 是两个数组的差的最大值

热点排行