清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx

上传人:b****2 文档编号:14593849 上传时间:2022-10-23 格式:DOCX 页数:93 大小:65.76KB
下载 相关 举报
清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx_第1页
第1页 / 共93页
清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx_第2页
第2页 / 共93页
清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx_第3页
第3页 / 共93页
清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx_第4页
第4页 / 共93页
清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx_第5页
第5页 / 共93页
点击查看更多>>
下载资源
资源描述

清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx

《清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx(93页珍藏版)》请在冰豆网上搜索。

清华大学严蔚敏数据结构课后题答案Word格式文档下载.docx

f)//求k阶斐波那契序列的第m项的值f

 

inttempd;

if(k<

2||m<

0)returnERROR;

if(m<

k-1)f=0;

elseif(m==k-1)f=1;

else

for(i=0;

i<

=k-2;

i++)temp[i]=0;

temp[k-1]=1;

//初始化

for(i=k;

=m;

i++)//求出序列第k至第m个元素的值

sum=0;

for(j=i-k;

j<

i;

j++)sum+=temp[j];

temp[i]=sum;

}

f=temp[m];

returnOK;

}//fib

分析:

通过保存已经计算出来的结果,此方法的时间复杂度仅为O(m^2).如果采用递归编程(大多数人都会首先想到递归方法),则时间复杂度将高达O(k^m).

1.18

typedefstruct{

 

char*sport;

enum{male,female}gender;

charschoolname;

//校名为'

A'

'

B'

C'

D'

或'

E'

char*result;

intscore;

}resulttype;

intmalescore;

intfemalescore;

inttotalscore;

}scoretype;

voidsummary(resulttyperesult[])//求各校的男女总分和团体总分,假设结果已经储存在result[]数组中

scoretypescore;

i=0;

while(result[i].sport!

=NULL)

switch(result[i].schoolname)

case'

:

score[0].totalscore+=result[i].score;

if(result[i].gender==0)score[0].malescore+=result[i].score;

elsescore[0].femalescore+=result[i].score;

break;

score.totalscore+=result[i].score;

if(result[i].gender==0)score.malescore+=result[i].score;

elsescore.femalescore+=result[i].score;

…… 

……

i++;

5;

i++)

printf("

School%d:

\n"

i);

Totalscoreofmale:

%d\n"

score[i].malescore);

Totalscoreoffemale:

score[i].femalescore);

Totalscoreofall:

%d\n\n"

score[i].totalscore);

}//summary

1.19

Statusalgo119(inta[ARRSIZE])//求i!

*2^i序列的值且不超过maxint

last=1;

for(i=1;

=ARRSIZE;

a[i-1]=last*2*i;

if((a[i-1]/last)!

=(2*i))reurnOVERFLOW;

last=a[i-1];

returnOK;

}//algo119

当某一项的结果超过了maxint时,它除以前面一项的商会发生异常.

1.20

voidpolyvalue()

floatad;

float*p=a;

Inputnumberofterms:

"

);

scanf("

%d"

n);

Inputthe%dcoefficientsfroma0toa%d:

n,n);

=n;

i++)scanf("

%f"

p++);

Inputvalueofx:

x);

p=a;

xp=1;

//xp用于存放x的i次方

sum+=xp*(*p++);

xp*=x;

Valueis:

sum);

}//polyvalue

第二章线性表

2.10

StatusDeleteK(SqList&

a,inti,intk)//删除线性表a中第i个元素起的k个元素

if(i<

1||k<

0||i+k-1>

a.length)returnINFEASIBLE;

for(count=1;

i+count-1<

=a.length-k;

count++)//注意循环结束的条件

a.elem[i+count-1]=a.elem[i+count+k-1];

a.length-=k;

}//DeleteK

2.11

StatusInsert_SqList(SqList&

va,intx)//把x插入递增有序表va中

if(va.length+1>

va.listsize)returnERROR;

va.length++;

for(i=va.length-1;

va.elem[i]>

x&

&

i>

=0;

i--)

va.elem[i+1]=va.elem[i];

va.elem[i+1]=x;

}//Insert_SqList

2.12

intListComp(SqListA,SqListB)//比较字符表A和B,并用返回值表示结果,值为正,表示A>

B;

值为负,表示A<

值为零,表示A=B

A.elem[i]||B.elem[i];

if(A.elem[i]!

=B.elem[i])returnA.elem[i]-B.elem[i];

return0;

}//ListComp

2.13

LNode*Locate(LinkListL,intx)//链表上的元素查找,返回指针

for(p=l->

next;

p&

p->

data!

=x;

p=p->

next);

returnp;

}//Locate

2.14

intLength(LinkListL)//求链表的长度

for(k=0,p=L;

next,k++);

returnk;

}//Length

2.15

voidListConcat(LinkListha,LinkListhb,LinkList&

hc)//把链表hb接在ha后面形成链表hc

hc=ha;

p=ha;

while(p->

next)p=p->

next=hb;

}//ListConcat

2.16

见书后答案.

2.17

StatusInsert(LinkList&

L,inti,intb)//在无头结点链表L的第i个元素之前插入元素b

p=L;

q=(LinkList*)malloc(sizeof(LNode));

q.data=b;

if(i==1)

q.next=p;

L=q;

//插入在链表头部

while(--i>

1)p=p->

q->

next=p->

next=q;

//插入在第i个元素的位置

}//Insert

2.18

StatusDelete(LinkList&

L,inti)//在无头结点链表L中删除第i个元素

if(i==1)L=L->

//删除第一个元素

next->

//删除第i个元素

}//Delete

2.19

StatusDelete_Between(Linklist&

L,intmink,intmaxk)//删除元素递增排列的链表L中值大于mink且小于maxk的所有元素

data<

=mink)p=p->

//p是最后一个不大于mink的元素

if(p->

next) 

//如果还有比mink更大的元素

q=p->

while(q->

maxk)q=q->

//q是第一个不小于maxk的元素

}//Delete_Between

2.20

StatusDelete_Equal(Linklist&

L)//删除元素递增排列的链表L中所有值相同的元素

p=L->

//p,q指向相邻两元素

next)

=q->

data)

//当相邻两元素不相等时,p,q都向后推一步

data==p->

data)

{

free(q);

q=q->

}

p=q;

//当相邻元素相等时删除多余元素

}//else

}//whil

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 人力资源管理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1