当前位置:网站首页>7-3 maximum sub segment and

7-3 maximum sub segment and

2022-06-24 23:31:00 White -

7-3 Maximum sub sum

Given n It's an integer ( It could be negative ) The sequence of components a[1],a[2],a[3],…,a[n], Find the sequence as a[i]+a[i+1]+…+a[j] The maximum value of the sum of the subsegments of . When all given integers are negative , Define sub segments and as 0.

The time complexity of the algorithm is required to be O(n).

Input format :
There are two lines of input :

The first line is n value (1<=n<=10000);

The second line is n It's an integer .

Output format :
Output maximum sub segment sum .

sample input :
Here's a set of inputs . for example :

6
-2 11 -4 13 -5 -2

sample output :
Here is the corresponding output . for example :

20

Code :

#include <stdio.h>
#include <stdlib.h>
int n;
int a[10100];
int vis[10100];
int findmax(int a,int b)
{
    
    return a>=b?a:b;
}
int main()
{
    
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
    int max1=0;
    int temp;
    for(int i=1;i<=n;i++)
    {
    
        if(temp>max1)
            max1=temp;
        if(temp<0)
            temp=a[i];
        else
            temp+=a[i];
    }
    printf("%d",max1);
    return 0;
}

202206222059 3、 ... and

原网站

版权声明
本文为[White -]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241820371209.html