数据结构课程课后习题集答案解?- 百度文库

4. 㷨

1һ㷨RepChar(s,x,y)˳sַx滻ַyҪռ临ӶΪO(1)

⣺Ҫ㷨ռ临ӶΪO(1)ֻܶԴsֱ滻ͷʼsһҵַx㽫滻yӦ㷨£

void RepStr(SqString &s,char x,char y) { }

int i;

for (i=0;i

if (s.data[i]==x)

s.data[i]=y;

2һ㷨жsԪǷΪеġ ⣺pqָs㣬pָʼ㣬q->datap->dataʱpqͬһ㣻򷵻0Ԫǵʱ1Ӧ㷨£

int increase(LinkString *s) { }

LinkString *p=s->next,*q; if (p!=NULL) { } return 1;

while (p->next!=NULL) { }

q=p->next;

p=q;

//ʱ0

return 0;

//qָ*pĺ

if (q->data>=p->data) else

3ʽṹ洢һsһ㷨sֵӴ

⣺贮ôͷĵ洢smaxƽ̨ȣɨ贮sһƽ̨ijnnmaxmaxΪnӦ㷨£

int maxlength(LinkString *s) {

int n,max=0;

LinkString *p=s->next,*q; while (p!=NULL) {

n=1;

q=p;p=p->next;

while (p!=NULL && p->data==q->data) { }

if (n>max) max=n;

n++; p=p->next;

ݽṹ̳

}

} return max;

ϻʵ4

ǿմst˳洢ṹ洢һ㷨󹫹Ӵݽвԡ

⣺Index㷨˼·˳st󹫹ӴstrӦij£

#include #include \ { } void main() {

SqString s,t,str;

Assign(s,\Assign(t,\printf(\SqString str;

int midx=0,mlen=0,tlen,i=0,j,k; while (i

for (i=0;i

str.length=mlen; return str;

//󹫹Ӵ

//󹫹ӴƵstr

str.data[i]=s.data[midx+i]; j=0; { } i++;

//ɨsеiַַ֮

while (j

if (s.data[i]==t.data[j]) //һӴ,s±Ϊi,Ϊtlen { } else j++;

tlen=1;

for (k=1;i+k

//ɨtеj+tlenַַ֮

&& s.data[i+k]==t.data[j+k];k++)

//ϴ󳤶߸midxmlen

tlen++; midx=i; mlen=tlen;

//(midx,mlen)󹫹Ӵ //iɨ贮s

//jɨ贮t

//˳򴮵Ļ㺯

SqString maxcomstr(SqString s,SqString t)

if (tlen>mlen)

}

printf(\

printf(\st󹫹Ӵstr\\n\str=maxcomstr(s,t); printf(\

ϰ5

1. ѡ

16070еĶάa[1..60,1..70]±1ʼΪ˳洢ַΪ10000ÿԪռ2洢Ԫô32е58еԪa[32,58]Ĵ洢ַΪ

A.16902 B.16904 C.14454 D.϶ A

2ѹ洢ĿҪΪˣ A.ü B.ԾԪصĴȡü C.ȥеĶԪ D.ٲҪĴ洢ռ D

3һnnĶԳƾѹ洢лΪڴ棬ѹ洢ǣ

A. n2 B. n2/2 C. n(n+1)/2 D. (n+1)2/2 C

4aһԳƾΪ˽ʡ洢Dzְһάb[1..n(n-1)/2]У±1ʼDzһԪai,jijһάb±kֵǣ

A.i(i-1)/2+j-1 B.i(i-1)/2+j C.i(i+1)/2+j-1 D.i(i+1)/2+j B

5һάA±ķΧ08±ķΧ15ÿԪڵ4ֽڴ洢洢ֽڱַ洢ԪA[0,1]ĵһֽڵĵַ0洢AһԪصĵһֽڵĵַǣ д洢A[3,5]A[5,3]ĵһֽڵĵֱַǣ ͣ д洢A[7,1]A[2,4]ĵһֽڵĵֱַǣ ͣ

A.28 B.44 C.76 D.92 E.108 F.116 G.132 H.176 I.184 J.188 𣺢H C E A F

6һάA±ķΧ16±ķΧ07ÿԪڵ6ֽڴ洢洢ֽڱַôǣ ֽڡ洢ԪA[1,0]ĵһֽڵĵַ0洢AһԪصĵһ

ݽṹ̳

ֽڵĵַǣ д洢A[2,4]ĵһֽڵĵַǣ д洢A[5,7]ĵһֽڵĵַǣ

A.12 B.66 C.72 D.96 E.114 F.120 G.156 H.234 I.276 J.282 K.283 L.288 𣺢L J C I

7ϡһѹ洢֣ A.άά B.Ԫɢ C.Ԫʮ D.ɢкʮ C

2.

1άA[c1..d1,c2..d2,c3..d3]c1d1c2d2c3d3У Ԫء (d1-c1+1)(d2-c2+1)(d3-c3+1)

2֪άA[m][n]Ϊʽ洢ÿԪռk洢ԪҵһԪصĴ洢ַLOC(A[0][0])A[i][j]ĵַǣ

LOC(A[0][0])+(ni+j)k 3άA[10][20]Ϊʽ洢ÿԪռһ洢ԪA[0][0]Ĵ洢ַ200A[6][12]ĵַǣ

326

4άA[10..20][5..10]Ϊʽ洢ÿԪռ4洢ԪA[10][5]Ĵ洢ַ1000A[18][9]ĵַǣ

1208

5һ10׶ԳƾAѹ洢ʽΪ洢Dz֣A[0][0]B[1]УA[8][5]Bеĵַǣ

42

6nǾA[1..n][1..n]ѹһάS[1..n(n+1)/2]УΪ洢A[i][j]ӦSеĴ洢λǣ

i(i-1)/2+j

7ϡԪʾУÿӦϡһԪأֱʾԪصģ

±ꡢ±Ԫֵ

3. 㷨

1ٶA[0..n-1]nԪжԪأһ㷨AеķԪƵAǰˡ

⣺ǰΪԪA[i]ӺǰҷԪA[j]A[i]A[j]нӦ㷨£

void move(ElemType A[],int n) {

int i=0,j=n-1;

ϵͷ779662525#qq.com(#滻Ϊ@)