标题:有个问题请教一下
只看楼主
lili3499
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2016-6-12
结帖率:14.29%
已结贴  问题点数:10 回复次数:3 
有个问题请教一下
这样的一道题:给定由n个整数(可能为负整数)组成的序列a1, a2,…, an, 求该序列连续子段和的最大值。当所有整数均为负整数时定义其最大子段和为0。
这个程序我是从网上下的,运行结果是正确的,但是该递归函数有个地方无法理解,请高人指点:
#include<iostream>
using namespace std;
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 main()
{
    int n,a[10000];
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i];
    cout<<MaxSubSum(a,0,n-1);
    return 0;
}
递归函数执行到这句就无法理解了int leftsum  = MaxSubSum(a, left, center);
                                int rightsum = MaxSubSum(a, center+1, right );   这里程序是怎样运行的,怎样才能得到最大子段和的呢?
搜索更多相关主题的帖子: 网上 最大值 center include 
2016-08-04 11:53
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:5 
随手写的,没验证

程序代码:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;

int MaxSubSum( const int buf[], size_t n )
{
    int maxsum = 0;
    int sum = 0;
    for( size_t i=0; i!=n; ++i )
    {
        sum = max( sum+buf[i], 0 );
        maxsum = max( sum, maxsum );
    }
    return maxsum;
}

int main( void )
{
    std::vector<int> buf;
    {
        size_t n;
        cin >> n;
        buf.resize( n );
        copy_n( std::istream_iterator<int>(cin), n, buf.begin() );
    }
    cout << MaxSubSum(&buf[0],buf.size()) << endl;

    return 0;
}

2016-08-04 16:04
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:5 
回复 楼主 lili3499
我这个可能写得啰嗦了一点仅供参考
#include<iostream>
using namespace std;
struct Num
{
    int n;
    Num*next;
};
class ChainNum
{
private:
    Num*head;
public:
    ChainNum()
    {
        if(head!=NULL)
        {
            head=NULL;
        }
    }
    ~ChainNum()
    {
        Num*p;
        if(head!=NULL)
        {
            p=head->next;
            delete head;
            head=p;
        }
    }
    void AddItem(int appended)
    {
        if(head==NULL)
        {
            head=new Num;head->n=appended;head->next=NULL;
            return;
        }
        Num*p,*p_new;p=head;
        while(p->next!=NULL)
            p=p->next;
        p_new=new Num;p_new->n=appended;p_new->next=NULL;
        p->next=p_new;
        return;
    }
    void AddItem()
    {
        int appended;cin>>appended;
        if(head==NULL)
        {
            head=new Num;head->n=appended;head->next=NULL;
            return;
        }
        Num*p,*p_new;p=head;
        while(p->next!=NULL)
            p=p->next;
        p_new=new Num;p_new->n=appended;p_new->next=NULL;
        p->next=p_new;
        return;
    }
    void DisplayAll()
    {
        cout<<endl;
        Num*p;p=head;
        while(p!=NULL)
        {
            cout<<p->n<<'\t';
            p=p->next;
        }
    }
    int ChainMax()
    {
        int biggest=0,sum=0;
        Num*begin,*p;begin=head;
        while(begin!=NULL)
        {
            p=begin;
            while(p!=NULL)
            {
                sum+=p->n;
                if(sum>biggest)
                    biggest=sum;
                p=p->next;
            }
            sum=0;
            begin=begin->next;
        }
        return biggest;
    }
};
int main()
{
    cout<<"请输入元素个数:";
    int count;
    cin>>count;
    cout<<"请依次输入数值,以回车键分隔:";
    ChainNum cn;
    while(count>0)
    {
        cn.AddItem();count--;
    }
    cout<<"你输入的全部元素为:";
    cn.DisplayAll();
    cout<<"\n连续子段最大和为:"<<cn.ChainMax()<<endl;
    return 0;
}
2016-08-04 23:34
lili3499
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2016-6-12
得分:0 
谢谢两位热心的版主!虽然问题还没有解决,我只是对该程序中的递归流程有疑问哦,不过还是很谢谢你们啦!
2016-08-05 15:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-467529-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.422279 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved