RSS发布话题

最大子序列和

jxk

jxk发表于50天 21小时 22分钟前
来源:acm.hdu.edu.cn  标签:交流

Google

#include <iostream>
using namespace std;


int find_sub(int *p,int number,int& start,int& end )//DP算法
{
   int cur_number=0,max_number=0,temp=0;
   start=end=0;
   for(int i=0;i<number;i++)
   {
     cur_number+=p;
     if(cur_number>max_number)
     { max_number=cur_number;
       start=temp;
       end=i;
     }
   else if(cur_number<0)
     {
      cur_number=0;
      temp=i+1;
     }
  }
return max_number;
}

int main()
{
  int max_sum,start,end,text_num,mark=0,a[10010];

  while( scanf("%d",&text_num),text_num)
  {
       for(int i=0;i<text_num;i++)//输入检查
       {
     scanf("%d",&a);
     if(a>=0) mark=1;
    }
    
       if(mark==0) printf("%d %d %dn",0,a[0],a[text_num-1]);//处理所有的数为负数上的情况
       else
    {
  max_sum=find_sub(a,text_num,start,end);
        printf("%d %d %dn",max_sum,a[start],a[end]);
       }
    
  }
return 0;
}

无法达到要求,希望各位大牛帮忙修改一下



Disclaimer: some contents on this website are collected through internet etc. Please notify if violated the original author's copyright and we will delete it immediately.
免责声明:本站部分文章来源于网络等其它媒体,如果侵犯了原作者的版权,请联系我们,本站将立即删除。

关注用户

    最近还没有登录用户关注过这篇文章…
暂无评论
共有 0 位网友发表了评论

评论