- 28
- 0
- 约12.89万字
- 约 98页
- 2016-10-12 发布于贵州
- 举报
数据结构严蔚敏题册 答案
第一章 绪论1.16 void print_descending(int x,int y,int z)//按从大到小顺序输出三个数{??scanf(%d,%d,%d,x,y,z);??if(xy) x-y; //-为表示交换的双目运算符,以下同??if(yz) y-z;??if(xy) x-y; //冒泡排序??printf(%d %d %d,x,y,z);}//print_descending 1.17 Status fib(int k,int m,int f)//求k阶斐波那契序列的第m项的值f{? ?int tempd;??if(k2||m0) return ERROR; ??if(mk-1) f=0;??else if (m==k-1 || m==k) f=1;??else??{? ? for(i=0;i=k-2;i++) temp[i]=0;? ? temp[k-1]=1;temp[k]=1; //初始化? ? sum=1;? ? j=0;? ? for(i=k+1;i=m;i++,j++) //求出序列第k至第m个元素的值? ?? ?temp[i]=2*sum-temp[j];? ? f=temp[m];??}??return OK;}//fib分析: k阶斐波那契序列的第m项的值f[m]=f[m-1]+f[m-2]+......+f[m-k]? ?? ???=f[m-1]+f[m-2]+......+f[m-k]+f[m-k-1]-f[m-k-1]? ?? ???=2*f[m-1]-f[m-k-1]所以上述算法的时间复杂度仅为O(m). 如果采用递归设计,将达到O(k^m). 即使采用暂存中间结果的方法,也将达到O(m^2).? ? 1.18 typedef struct{? ?? ?? ?? ?? ?? ???char *sport;? ?? ?? ?? ?? ?? ???enum{male,female} gender;? ?? ?? ?? ?? ?? ???char schoolname; //校名为A,B,C,D或E? ?? ?? ?? ?? ?? ???char *result;? ?? ?? ?? ?? ?? ???int score;? ?? ?? ?? ?? ?? ?} resulttype; typedef struct{? ?? ?? ?? ?? ?? ???int malescore;? ?? ?? ?? ?? ?? ???int femalescore;? ?? ?? ?? ?? ?? ???int totalscore;? ?? ?? ?? ?? ?? ?} scoretype; void summary(resulttype result[ ])//求各校的男女总分和团体总分,假设结果已经储存在result[ ]数组中{??scoretype score[MAXSIZE];??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;? ?? ???else score[ 0 ].femalescore+=result[i].score;? ?? ???break;? ?? ?case B:? ?? ???score[ 0 ].totalscore+=result[i].score;? ?? ???if(result[i].gender==0) score[ 0 ].malescore+=result[i].score;? ?? ???else score[ 0 ].femalescore+=result[i].score;? ?? ???break;? ?? ?……? ? ……? ? ……? ? }? ? i++;??}??for(i=0;i5;i++)??{? ? printf(School %d:\n,i);? ? printf(Total score of male:%d\n,score[i].malescore);? ? printf(Total score of female:%d\n,score[i].femalescore);? ? printf(Total score of al
原创力文档

文档评论(0)