数据结构:Python语言描述吕云翔习题答案.pdf
《数据结构:Python语言描述吕云翔习题答案.pdf》由会员分享,可在线阅读,更多相关《数据结构:Python语言描述吕云翔习题答案.pdf(132页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 数据结构:Python语言描述参考答案习题11.A2.C3.C4.A5.C6.B7.B8.C9.D10.C11.A12.B13.C14.D、1.集合;线性结构;树形结构;图形结构2.集合;线性结构;树形结构;图形结构3.有穷性;确切性;输入;输出;可行性4.时间和空间复杂度5.一对一;一对多;多对多6.逻辑关系7.没有;一8.一个;一个;后继;任意个9.任意个10.物理11.时间复杂度;空间复杂度12.问题规模三、1.#simple but ineffic ientdef prime(m):flag=Trueif(m=1):flag=Falsefor i in range(2,m):if(m
2、%i=0):flag=Falsebreakreturn flagn=int(input()print(prime(n)2.n=int(input()fac t=1tot=0for i in range(l,n+1):fac t*=itot+=fac tprint(sum=%d%tot)3.#same as O2.py#bec ause Q-2 is same as Q-34.def max(n):sum=0i=0while(sum n):i+=1sum+=ireturn in=int(input()print(i=%d*%max(n)5.#n*n multiplic ation table#n
3、 less than 10n=int(input()for i in range(1,n+1):for j in range(i,n+1):print(%d*%d=%0 2 d,%(i,j,i*j),end=n)print。习题21.B2.A3.C4.C5.B6.B7.A二、1.n+l ;n2.顺序;链式3.O(n);O(n)4.0(1);0(1)5.O(n);0(1)6.0(1);0(1)7.0(1);O(n)c lass SqList(objec t):def_ init_(self,maxSize):self.c urLen=0self.maxSize=maxSizeself.listl
4、tem=None*self.maxSizedef c lear(self):self.c urLen=0def isEmpty(self).return self.c urLen=0def length(self)return self.c urLendef get(self,i):if i self.c urLen-1:raise Exc eptionC4ndex out of range1)return self.listltemfidef insert(self,i,x):passOl-pynew remove func tiontudef remove(self,i):if i sel
5、f.c urLen-1:raise Exc eption(index out of range)self.c urLen-=1for k in range(i,self.c urLen):self.listltemfk=self.listltemk+lfree half of the storage spac e of the arraywhile spac e usage less than 40 perc enttnif seif.c urLen/seif.maxSize 0.4:self.maxSize=int(self.maxSize/2)temp=self.listltemself.
6、listitem=None*self.maxSizefor i in range(self.c urLen):self.listltemfi=tempfidef indexOf(self,x):passdef display(self):fbr i in range(self.c urLen):print(self.listltemi,end=)print。)2.c lass Sequenc eSet(objec t):O2.pyc onstruc torH ldefinit_(self,arr):self.maxSize=int(len(arr)*1.5)self.setArray=None
7、J*self.maxSizeself.c urLen=0c ount=0for i in range(len(arr):if arrlij not in self.setAnay:self.setArrayc ount=arric ount+=1self.c urLen=c ountdef c lear(self):self.c urLen=0def isEmpty(self):return self.c urLen=0def length(self)return self.c urLendef get(self,i):passdef insert(self,i,x):passdef remo
8、ve(self,i):passdef indexOf(self,x):passdef display(self).for i in range(self.c urLen):print(self.setArrayi,end=)print()arr=input().split()test=Sequenc eSet(arr)test.displayOtest c ase#input3 2 6 4 6 4 3 9 8 1 8 4 7 4 2 0 1 1 29#output3 2 6 4 9 8 1 70n i3.c lass SqList(objec t):def_ init_(self,arr):s
9、elf.maxSize=int(len(arr)*1.5)self.listltem=None*self.maxSizeself.c urLen=0c ount=0for i in range(len(arr):self.listltemc ount=arric ount+=1self.c urLen=c ountdef display(self)*fbr i in range(self.c urLen):print(self.listltemi,end=)print。)O3.pyreturn max number of listitemHIdef getMax(self)*maxNum=se
10、lf.listItemOfor i in range(self.c urLen):if maxNum self.listltemij:maxNum=self.listltemireturn maxNumarr=input().split(*)test=SqList(arr)print(test.getMax()test c ase#input7.05 6.34 9.55 4.10 3.83 0.20 9.04 2.68 3.99 4.86#output9.55nt4.c lass Sequenc eSet(objec t):definit(self,arr):self.maxSize=int(
11、len(arr)*1.5)self.setAiTay=NoneJ*self.maxSizeself.c urLen=0c ount=0for i in range(len(arr):if aiTiJ not in self.setArray:self.setArrayc ountl=arric ount+=1self.c urLen=c ountdef display(self)*for i in range(self.c urLen):print(self.setArrayi,end=)print。)O4.pyc opy into c urrent setArraydef c opy(sel
12、f,s):temp=self.setArrayself.setArray=None*(self.maxSize+s.maxSize)c ount=0for i in range(self.c urLen+s.c urLen):if i self.c urLen:sei f.set Array i=tempielif s.setArrayi-self.c urLenJ not in self.setArray:self.setArrayself.c urLen+c ount=s.setArrayi-self.c urLenc ount+=1self.c urLen+=c ountarrl=inp
13、ut(,enter arrlXn.splitC)setl=Sequenc eSet(arrl)setl.displayOarr2=input(*enter air2n).split()set2=Sequenc eSet(arr2)set2.display()setl.c opy(set2)print(after c opying)setl.displayOtest c ase#input16 11 24 3 10 1521 199 1912 1 13 12 17 10 23 2 19 2#output16 II 24 3 10 15 21 19912 1 13 17 10 23 2 1916
14、11 24 3 10 15 21 199 12 1 13 17 23 25.c lass SqList(objec t):definit(self,arr):self.maxSize=int(len(arr)*1.5)self.listltem=None*self.maxSizeself.c urLen=0c ount=0for i in range(len(arr):self.listltemc ount=an-ijc ount+=1self.c urLen=c ountdef display(self).for i in range(self.c urLen):print(self.lis
15、tltemi,end=)print()O5.pyA-B=A.diff(B)An element that exists in A but does not exist in Bdef diff(self,arr):temp=for i in range(self.c urLen):if self.listltemi not in arr.listltem:temp.append(self.listltemi)return SqList(temp)n,testM,arrl=inputfenter setln).split()setl=SqList(arrl)arr2=input(*enter s
16、et2n,).split(,*)set2=SqList(arr2)retl=setl.diff(set2)print(setl-set21)retl.display()ret2=set2.diff(setl)print(set2-set 1f)ret2.display()#input1611 243 10 1521 199 1912 1 13 12 17 10 23 2 192#outputsetl-set216 11 243 15 21 9set2-setl12 1 13 12 17 23 2 2!(6.c lass Node(objec t):definit_(self,data=None
17、,next=None):self.data=dataself.next=nextc lass LinkList(objec t):def_ init_(self):self.head=Node()#order True or Falsedef c reate(self,1,order):if order:self.c reate_tail(l)else:self.c reate_head(l)def c reate_tail(self,1):for item in 1:self.insert(self.length(),item)def c reate_head(self,1):for ite
18、m in 1:self.insert(O,item)def c lear(selO:self.head.data=Noneself.head.next=Nonedef isEmpty(self):return self.head.next=Nonedef length(self):p=self.head.nextlength=0while p is not None:p=p.nextlength+=1return length#get i-th elementdef get(self,i):p=self.head.nextj=owhile j i or p is None:raise Exc
19、eption()return p.data#insert x as the i-th elementdef insert(self,i,x):p=self.headJ=-1while p is not None and j i-1 or p is None:raise Exc eption()s=Node(x,p.next)p.next=s#remove the i-th elementdef remove(self,i):p=self.headj=-1while p is not None and j i-1 or p.next is None:raise Exc eption()p.nex
20、t=p.next.nextdef indexOf(self,x):p=self.head.nextj=owhile p is not None and not(p.data=x):p=p.nextj+=1if p is not None:return jelse:return-1def display(self):p=self.head.nextwhile p is not None:print(p.data,end=)p=p.nextprint。)M,O6.pyA-B=A.diff(B)An element that exists in A but does not exist in Bde
21、f diff(self,arr):p=self.headtemp=LinkList()c ount=0while p.next is not None:if aiT.indexOf(p.next.data)=-1:temp.insert(c ount,p.next.data)c ount+=1p=p.nextreturn tempHtest,narrl=inputfenter setlVn.splitC*)setl=Link.List()setl.c reate(arr 1,True)arr2=input(*enter set2n,).split(,*)set2=LinkList()set2.
22、c reate(arr2,True)retl=setl.diff(set2)print(setl-set2)retl.displayOret2=set2.diff(setl)print(,set2-setl)ret2.display()test c ase#input208 11 125 16 13 13 1012 14 11 15 3 85 3 14 15#outputsetl-set220 16 13 13 10set2-setl14 15 3 3 14 15tn7-a.c lass SqList(objec t):def _ init_(self,maxSize):passomit ot
23、her func tions H,”07-a.pyEstatic methoddef Josephus(n,m,s):arr=None*nfor i in range(n):arri=i+1ptr=s-1for i in range(n):for j in range(m-l):while arrptr=-1:ptr=(ptr+1)%nc ontinueptr=(ptr+1)%nwhile arrfptr=-1:ptr=(ptr+1)%nc ontinueprint(arrptr,end=)arr ptr=-1print。)ifname=main:n=int(input()m=int(inpu
24、t()s=int(input()SqList.Josephus(n,m,s)test c ase 1#input841#output4 8 5 2 1 3 7 6test c ase 2#input1879output154 11 1 9 18 103 148657 132 12 16 177-b.c lass Node(objec t):definit_(self,data=None,next=None):self.data=dataself.next=nextc lass LinkList(objec t):def_ init_(self):passomit other func tion
25、sH,07-b.py static methoddef Josephus(n,m,s):head=Node(1)p=q=headfor i in range(n-l):q=Node(i+2)p.next=qp=qq.next=headfbr i in range(s-l):p=p.nextq=pfor i in range(n):for j in range(m-l):p=p.nextq=pq=q.nextprint(q.data,n,end=)p.next=q.nextprint。)n,testM,if _ name_ =_ mainn=int(input()m=int(input()s=i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 Python 语言 描述 吕云翔 习题 答案
限制150内