7-1 最大子列和问题

7-1 最大子列和问题,第1张

7-1 最大子列和问题

#include
#include
#include
typedef struct node
{
    int data;
    struct node *next;
}node;

node* create(node*&L,int k)
{
    L=(node*)malloc(sizeof(node));//头节点
    node *p;
    L->next=NULL;
    node*r;
    r=L;
    int i;
    for(i=0;i     {
        p=(node*)malloc(sizeof(node));
        scanf("%d",&(p->data));
        p->next=NULL;
        r->next=p;
        r=p;
    
        
    }
return L;
     
}
int maxx(node*L)
{
    int max=0;
    int sum=0;
    node*p=L->next;
    while(p!=NULL)
    {
        sum+=p->data;
        if(sum<0)
        sum=0;
        if(sum>max)
        max=sum;
        p=p->next; 
    }
    return max;
}
int main()
{
    node*L;
    int k;
    scanf("%d",&k);
    create(L,k);
    
    printf("%d",maxx(L));
    
    
    return 0;
 } 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zaji/5610763.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存