最大连续子序列 (13分)
题目要求:
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。
具体的输入输出格式规定如下:
输入格式:测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 50
),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。
输出格式:对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样
例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素
。
输入样例:

6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
输出样例:

20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
要求:
完整的输入输出,以及异常处理
一些异常处理
源代码如下:
/* zju.h
Copyright (c) 2002, 2006 by ctu_85
All Rights Reserved.
*/
#include "stdio.h"
#include "string.h"
#include "malloc.h"
#define maxnum 50
#define lowest -65536
struct List
{
int num;
int maxhead;
int maxend;
int max;
int allneg;
int data[maxnum];
struct List *next;
};
struct List *Create();
int Print(struct List *);
int Sort(struct List *);
int main()
{
int i;
struct List *head;
head=Create();
Sort(head);
Print(head);
return 1;
}
struct List *Create()
{
struct List *head,*pre,*p;
int i=0,j,num,base=1,temp=0,headstatus=0,negtive,t;
char c;
do
{
printf("Please input the number of the array:\n");
scanf("%d",&num);
if(num!=0)
{
p=(struct List *)malloc(sizeof(struct List));
p->maxhead=0;
p->maxend=0;
p->max=lowest;
p->allneg=0;
if(headstatus==0)
{
head=p;
pre=p;
p->num=num;
i=0;
temp=0;
base=1;
negtive=0;
putchar(getchar());
while((c=getchar())!='\n')
if(c==' ')
{
t=temp*((-2)*negtive+1);
p->data[i]=t;
if(t>=0)
p->allneg=1;
i++;
temp=0;
base=1;
negtive=0;
}
else
{
if(c!='-')
{
temp*=base;
temp+=c-'0';
base*=10;
}
else
negtive=1;
}
t=temp*((-2)*negtive+1);
p->data[i]=t;
if(t>=0)
p->allneg=1;
headstatus++;
}
else
{
pre->next=p;
p->num=num;
i=0;
temp=0;
base=1;
negtive=0;
putchar(getchar());
while((c=getchar())!='\n')
if(c==' ')
{
t=temp*((-2)*negtive+1);
p->data[i]=t;
if(t>=0)
p->allneg=1;
i++;
temp=0;
base=1;
negtive=0;
}
else
{
if(c!='-')
{
temp*=base;
temp+=c-'0';
base*=10;
}
else
negtive=1;
}
t=temp*((-2)*negtive+1);
p->data[i]=t;
if(t>=0)
p->allneg=1;
headstatus++;
pre=pre->next;
}
}
}
while(num!=0);
p->next=NULL;
return head;
}
int Sort(struct List *t)
{
int max,i,j,temp,head,end;
while(t!=NULL)
{
head=0;
end=0;
max=lowest;
for(i=0;i<t->num;i++)
{
temp=0;
for(j=i;j<t->num;j++)
{
temp+=t->data[j];
if(temp>max)
{
max=temp;
head=i;
end=j;
}
}
}
t->maxhead=head;
t->maxend=end;
t->max=max;
t=t->next;
}
return 1;
}
int Print(struct List *t)
{
int i,j,k;
if(t==NULL)
{
printf("Empty input!");
return 0;
}
else
{
while(t!=NULL)
{
i=t->maxhead;
j=t->maxend;
k=t->num;
if(t->max>0)
{
printf("result:%d,%d,%d",t->max,t->data[i],t->data[j]);
printf("\n");
t=t->next;
}
else
{
if(t->allneg==0)
{
printf("result:%d,%d,%d",0,t->data[0],t->data[k-1]);
printf("\n");
t=t->next;
}
else
{
printf("result:%d,%d,%d",0,0,0);
printf("\n");
t=t->next;
}
}
}
return 1;
}
}
了解更多浙大研究生复试解答请点击
http://blog.csdn.net/ctu_85/archive/2006/10/15/1334936.aspx
http://blog.csdn.net/ctu_85/archive/2006/10/16/1336101.aspx
http://blog.csdn.net/ctu_85/archive/2006/10/30/1357145.aspx
浙江大学ACM试题解答(四月)
http://blog.csdn.net/ctu_85/archive/2007/04/24/1576831.aspx
浙江大学ACM试题解答(三月)
http://blog.csdn.net/ctu_85/archive/2007/03/20/1535556.aspx
麻省理工算法导论翻译
http://blog.csdn.net/ctu_85/archive/2007/06/08/1643179.aspx
华容道游戏与算法
http://blog.csdn.net/ctu_85/archive/2007/05/15/1610722.aspx
中国象棋对战程序C语言源代码
http://blog.csdn.net/ctu_85/archive/2007/05/04/1596351.aspx
软件可行性报告
http://blog.csdn.net/ctu_85/archive/2006/06/06/775894.aspx
软件需求分析报告
http://blog.csdn.net/ctu_85/archive/2006/06/06/775892.aspx
发表于 @ 2006年10月31日 02:05:00|评论(loading...)|编辑