02

    xiaoxiao2025-04-08  24

    #include<stdio.h> #include<stdlib.h> typedef struct{ 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 minus int 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 status while(i<n){ nowsum+=list[i]; if(nowsum<0) { nowsum=0; tempindex=i+1; } //!!!!!distinguish else-if/if //else if under status : nowsum>0 || nowsum=0 else if(nowsum>maxsum){ maxsum=nowsum; out.first=tempindex; out.end=i; } i++; }//while if(maxsum<0){ maxsum=0;//all the number are minus } out.maxsum=maxsum; return out; } int main(){ 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"); } else printf("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 9 9 9 D:\workspace\C\struct_pat>a please input the length of list : 6 input numbers divided by space: -1 0 0 0 2 3 your list is : -1 0 0 0 2 3 the answer is 5 0 3
    最新回复(0)