#include<stdio.h>#include<stdlib.h>typedefstruct{int maxsum;int first;int end;}form;
form online(int list[],int n){
form out;int nowsum=0;int i=0;int maxsum=-1;//two functions : for status that all the numbers are "0" or minusint tempindex=0;//always signs the first item of the current Non-negative column(not the maxcolumn)//regard every Non-negative column has the possibility to become the maxcolumn//include the subset like "0 0 0 2 3"
out.first=0;
out.end=n-1;if(n==1&&list[0]>0) maxsum=list[0];//border statuswhile(i<n){
nowsum+=list[i];if(nowsum<0){
nowsum=0;
tempindex=i+1;}//!!!!!distinguish else-if/if//else if under status : nowsum>0 || nowsum=0elseif(nowsum>maxsum){
maxsum=nowsum;
out.first=tempindex;
out.end=i;}
i++;}//whileif(maxsum<0){
maxsum=0;//all the number are minus}
out.maxsum=maxsum;return out;}intmain(){int N,i,result;printf("please input the length of list :\n");scanf("%d",&N);int*l;//dynamic array
l=(int*)malloc(sizeof(int)*N);if(l){printf("input numbers divided by space:\n");for(i=0;i<N;i++)scanf("%d",l+i);printf("\n");printf("your list is :\n");for(i=0;i<N;i++)printf("%d\t",l[i]);printf("\n");}elseprintf("error!");
form answer=online(l,N);printf("\nthe answer is %d %d %d",answer.maxsum,l[answer.first],l[answer.end]);free(l);
l=NULL;}
please input the length of list :1
input numbers divided by space:9
your list is :9
the answer is 999
D:\workspace\C\struct_pat>a
please input the length of list :6
input numbers divided by space:-100023
your list is :-100023
the answer is 503