毕业设计,毕业论文代写。专业水平。钻石水准,黄金品质。

计算机专业毕业设计,论文,设计代写。电邮:elevenor@gmail.com。专业水平,质优价廉。

原创 浙江大学计算机系硕士研究生复试题目解答(4)收藏

新一篇: 基于Intel多核集群系统的市政建设全景仿真预测平台--——可行性评估报告 | 旧一篇: 浙江大学计算机系硕士研究生复试题目解答(3)

最大连续子序列 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个整数,中间用空格分隔。当K0时,输入结束,该用例不被处理。
输出格式:对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号ij最小的那个(如输入样
例的第23组)。若所有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...)|编辑

新一篇: 基于Intel多核集群系统的市政建设全景仿真预测平台--——可行性评估报告 | 旧一篇: 浙江大学计算机系硕士研究生复试题目解答(3)

评论

#fli2000 发表于2007-03-24 09:51:48  IP: 219.140.120.*
sort的算法不好,时间复杂度为n!
下面有一种算法(对应函数find),时间复杂度为n。
#include <stdio.h>

void find(int *array,int i,int length,int *low,int *high,int *record)

{

int sum=0;

int tmp_low;

int first=1;

while ((i<length)&&(array[i]<0))

++i;

tmp_low=i;

while (i<length&&sum>=0)

{

sum+=array[i];

if (sum>*record)

{

if (first)

{

*low=tmp_low;

first=0;

}

*high=i;

*record=sum;

}

++i;

}

if (i<length)

find (array,i,length,low,high,record);

}



char *strtonum(char *str,int *array,int i)

{

int j,base=1,count=0;

int plus;

while (*str==' '&&*str!='\0')

++str;

switch(*str)

{

case '+':

{

plus=1;

str++;

break;

}

case '-':

{

plus=0;

str++;

break;

}

default:plus=1;

}

while (*str!=' '&&*str!='\0')

{

str++;

count++;

}

for(j=0;j<count;j++)

{
#fli2000 发表于2007-03-24 09:52:58  IP: 219.140.120.*
*(array+i)+=base*(*(str-j-1)-'0');

base*=10;

}

if (!plus)

*(array+i)=0-*(array+i);

printf("the num is:%d\n",*(array+i));

return str;

}



void main()

{

int result=0,high=0,length=0;

int low=-1;

int i=0;

int array[50];

char num[200],len[5],*str_num;

for (i=0;i<50;i++)

array[i]=0;

i=0;

printf("Please input the numbers of array:\n");

gets(len);

strtonum(len,&length,0);

printf("the length is:%d\n",length);

str_num=gets(num);

puts(str_num);

printf("\n");

while (i<length)

{

str_num=strtonum(str_num,array,i);

printf("th is %d\n",i);

++i;

}

printf("the array is:");

i=0;

while (i<length)

printf("%d ",array[i++]);

printf("\n");

find(array,0,length,&low,&high,&result);

printf("The result is:\nsum=%d,low=%d,high=%d\n",result,low,high);



}

#fli2000 发表于2007-03-24 14:16:54  IP: 219.140.116.*
睡觉的时候发现sort的时间复杂度是n^2。
发表评论  


登录
Csdn Blog version 3.1a
Copyright © ctu_85