02

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值