数据结构习题答案-第一章

1. 本文是对严蔚敏《数据结构(c语言版)习题集》一书中所有算法设计题目的解决方案,主要作者为一具.以下网友:biwier,szm99,siice,龙抬头,iamkent,zames,birdthinking,lovebuaa等为答案的修订和完善工作提出了宝贵意见,在此表示感谢;
2.

);
3.

: 5.20, 10.40;
4.

;
5.

:  yi-ju@263.net

1.16

void print_descending(int x,int y,int z)//按从大到小顺序输出三个数
{
scanf("%d,%d,%d",&x,&y,&z);
if(x<y) x<->y; //<->

if(y<z) y<->z;
if(x<y) 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(k<2||m<0) return ERROR;
if(m<k-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++) //

temp[i]=2*sum-temp[j];
f=temp[m];
}
return OK;
}//fib

=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]

1.18

typedef struct{
char *sport;
enum{male,female} gender;
char schoolname; //

'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;i<5;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 all:%d/n/n",score[i].totalscore);
}
}//summary

1.19

Status algo119(int a[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)) reurn OVERFLOW;
last=a[i-1];
return OK;
}
}//algo119

1.20

void polyvalue()
{
float temp;
float *p=a;
printf("Input number of terms:");
scanf("%d",&n);
printf("Input value of x:");
scanf("%f",&x);
printf("Input the %d coefficients from a0 to a%d:/n",n+1,n);
p=a;xp=1;sum=0; //xp

for(i=0;i<=n;i++)
{
scanf("%f",&temp);
sum+=xp*(temp);
xp*=x;
}
printf("Value is:%f",sum);
}//polyvalue

• 评论

• 下一篇