Data structure largest sub-columns and problems

Given a sequence consisting of K integers {N​1, N2, …, N​K​ }, the "continuous sub-column" is defined as {Ni, Ni+1​​, …, Nj}, where 1≤i≤ j≤K. "Maximum sub-column sum" is defined as the largest of the sum of all consecutive sub-column elements. For example, given the sequence {-2, 11, -4, 13, -5, -2 }, its consecutive sub-columns {11, -4, 13} have the largest sum 20. Now you are required to write a program to calculate the maximum sub-column sum of a given sequence of integers.

This question aims to test the performance of various algorithms in various data situations. The characteristics of each group of test data are as follows:

Data 1: Equivalent to the sample, the test is basically correct;
Data 2: 102 random integers;
Data 3: 103 random integers;
Data 4: 104 random integers;
Data 5: 105 random integers;
Input format:
input The first line gives a positive integer K (≤100000); the second line gives K integers separated by spaces.

Output format:
output the largest sub-column sum in a row. If all integers in the sequence are negative, 0 is output.

Input sample:
6
-2 11 -4 13 -5 -2
Output sample:
20

#include<stdio.h>
int main()
{
    
    
	int n,i,a[100000]={
    
    0},sum=0,max=0;
	scanf("%d",&n);
	for(i = 0;i < n;i++)
	{
    
    
		scanf("%d",&a[i]);
	}
	for(i = 0;i < n;i++)
	{
    
    
		sum += a[i];
		if(sum > max)
		{
    
    
			max = sum;
		}
		else if(sum < 0)
		{
    
    
			sum = 0;
		}
	}
	printf("%d",max);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45728434/article/details/108904707