严蔚敏《数据结构c语言版习题集》算法设计题答案.docx
- 文档编号:276878
- 上传时间:2022-10-08
- 格式:DOCX
- 页数:27
- 大小:21.48KB
严蔚敏《数据结构c语言版习题集》算法设计题答案.docx
《严蔚敏《数据结构c语言版习题集》算法设计题答案.docx》由会员分享,可在线阅读,更多相关《严蔚敏《数据结构c语言版习题集》算法设计题答案.docx(27页珍藏版)》请在冰豆网上搜索。
严蔚敏《数据结构c语言版习题集》算法设计题答案
严蔚敏《数据结构(c语言版)习题集》算法设计题答案[1]
严蔚敏《数据结构
(c语言版
)习题集》算法设计题答案
第一章绪论
1.16
voidprint_descending(intx,inty,intz)//按从大到小顺序输出三个数
{
scanf("%d,%d,%d",&x,&y,&z);
if(x
以下同
if(y
if(x
printf("%d%d%d",x,y,z);
}//print_descending
1.17
Statusfib(intk,intm,int&f)//求
k阶斐波那契序列的第
m项的值
f
{
inttempd;
if(k<2||m<0)returnERROR;
if(m elseif(m==k-1)f=1; else { for(i=0;i<=k-2;i++)temp[i]=0; temp[k-1]=1;//初始化 for(i=k;i<=m;i++)//求出序列第 k至第 m个元素的值 { sum=0; for(j=i-k;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; typedefstruct{ intmalescore; intfemalescore; inttotalscore; }scoretype; voidsummary(resulttyperesult[])//求各校的男女总分和团体总分 假设结果已经储存在 result[] 数组中 { scoretypescore; i=0; while(result[i].sport! =NULL) { switch(result[i].schoolname) { case'A': score[0].totalscore+=result[i].score; if(result[i].gender==0)score[0].malescore+=result[i].score; elsescore[0].femalescore+=result[i].score; break; case'B': score.totalscore+=result[i].score; if(result[i].gender==0)score.malescore+=result[i].score; elsescore.femalescore+=result[i].score; break; …… ………… } i++; } for(i=0;i<5;i++) { printf("School%d: \n",i); printf("Totalscoreofmale: %d\n",score[i].malescore); printf("Totalscoreoffemale: %d\n",score[i].femalescore); printf("Totalscoreofall: %d\n\n",score[i].totalscore); } }//summary Statusalgo119(inta[ARRSIZE])//求 i! *2^i序列的值且不超过 maxint { last=1; for(i=1;i<=ARRSIZE;i++) { 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; printf("Inputnumberofterms: "); scanf("%d",&n); printf("Inputthe%dcoefficientsfroma0toa%d: \n",n,n); for(i=0;i<=n;i++)scanf("%f",p++); printf("Inputvalueofx: "); scanf("%f",&x); p=a;xp=1;sum=0;//xp用于存放 x的 i次方 for(i=0;i<=n;i++) { sum+=xp*(*p++); xp*=x; } printf("Valueis: %f",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; returnOK; }//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; returnOK; }//Insert_SqList 2.12 intListComp(SqListA,SqListB)//比较字符表 A和 B,并用返回值表示结果 值为正,表示 A>B; 值为负,表示 A 表示 A=B { for(i=1;A.elem[i]||B.elem[i];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;p->next;p=p->next,k++); returnk; }//Length 2.15 voidListConcat(LinkListha,LinkListhb,LinkList&hc)//把链表 hb接在 ha后面形成链表 hc { hc=ha;p=ha; while(p->next)p=p->next; 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;//插入在链表头部 } else { while(--i>1)p=p->next; q->next=p->next;p->next=q;//插入在第 i个元素的位置 }}//Insert 2.18 StatusDelete(LinkList&L,inti)//在无头结点链表 L中删除第 i个元素 { if(i==1)L=L->next;//删除第一个元素 else { p=L; while(--i>1)p=p->next; p->next=p->next->next;//删除第 i个元素 } }//Delete 2.19 StatusDelete_Between(Linklist&L,intmink,intmaxk)//删除元素递增排列的链表 L中值大于 mink且小于 maxk的所有元素 { p=L; while(p->next->data<=mink)p=p->next;//p是最后一个不大于 mink的元素 if(p->next)//如果还有比 mink更大的元素 { q=p->next; while(q->data maxk的元素 p->next=q; } }//Delete_Between 2.20 StatusDelete_Equal(Linklist&L)//删除元素递增排列的链表 L中所有值相同的元素 { p=L->next;q=p->next;//p,q指向相邻两元素 while(p->next) { if(p->data! =q->data) { p=p->next;q=p->next;//当相邻两元素不相等时,p,q都向后推一步 } else { while(q->data==p->data) { free(q); q=q->next; } p->next=q;p=q;q=p->next;//当相邻元素相等时删除多余元素 }//else }//while }//Delete_Equal 2.21 voidreverse(SqList&A)//顺序表的就地逆置 { for(i=1,j=A.length;i A.elem[i]<->A.elem[j]; }//reverse 2.22 voidLinkList_r
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构c语言版习题集 严蔚敏 数据结构 语言版 习题集 算法 设计 答案