第三單元練習(xí)及參考答案1.假設(shè)有如下功能的演唱會門票預(yù)訂系統(tǒng):按先到者先訂的規(guī)則進(jìn)行預(yù)訂當(dāng)門票都預(yù)訂完后,允許在隊(duì)伍中等待退票請畫出算法流程圖并編程實(shí)現(xiàn)排隊(duì)預(yù)訂及等候退票功能。參考答案流程圖如下所示;    參考代碼如下所示:#!/usr/bin/python#-*-coding:utf-8-*- total_tickets=3  #假設(shè)總票數(shù)為3user_in_the_queue=[]  #定義排隊(duì)隊(duì)列,用列表模擬 def get_ticket(user_name,amount)#如果還有票,返回購票數(shù)#否則繼續(xù)排隊(duì)  global total_ tickets  global user_in_the_queue  left=total_tickets-amount  if left>-1:    print( user_name, "get",amount, " tickets successfully)    total_tickets=total_tickets -amount    returnelse:print( user_name, "can not get",amount, "tickets, wait in the queue")user_in_the_queue.append(user_name) def get_return_ticket( user_name, amount):    global user_in_the_queue    first_user_in_the_queue=user_in_the_queue[len(user_in_the_queue)-1]    if first_user_in_the_queue ==user_name:        get_ticket( user_name, amount)else:print( user_name, "can not get ticket first, user in the queue now)    user_in_the__queue.append( user_name)  def main():user_name_a ="BoyAuser_name_b ="BoyBuser_name_c ="BoyCget_ticket(user_name_a, 3)get_ticket(user_name_b, 3)  return_ticket(3)  get_return_ticket(user_name_c, 1)  get_return_ticket(user_name_b, 3)  def return_ticket(amount):  global total_tickets  total_tickets=total_tickets+amountif _name_==_main_'main()2.四方向迷官問題求解:假設(shè)有8X8的迷官如下所示(迷官用二維數(shù)組存放,可查閱資料了解二維數(shù)組),1表示障礙不能通行,0表示通道可以通行。求解方法為:從人口開始出發(fā)沿著某一個(gè)方向向前試探,若能夠通行,則繼續(xù)試探前行:如果遇到障礙,馬上原路返回,并在該路口做上記號,換另一個(gè)方向繼續(xù)試探前行;重復(fù)上述步驟直到到達(dá)迷官的出口后結(jié)束。由于迷官錯(cuò)綜復(fù)雜,靠人力來搜索迷官的所有通路是很費(fèi)時(shí)、費(fèi)力的一件事,因此可以借助計(jì)算機(jī)編程來完成。入口0010001000100010000011000111000000010000010001000111011000001000出口畫出求解迷官問題中,棧的變化過程。設(shè)計(jì)解四方向迷宮問題的算法并編程實(shí)現(xiàn)。提示:在編程時(shí),為避免考慮第一行不能朝上走、第一列不能朝左走,最后一列不能朝右走,最下一行不能朝下走,可以在迷官外圍設(shè)置一圈圍墻,這樣所有的位置都可考慮四個(gè)方向,圍墻等同于障礙(1表示),如下所示。1111111111100100010110010001011000011001101110000110001000011010001001101110110110000100011111111111參考答案:    參考代碼如下所示#-*-coding:utf-8-*-#!/usr/bin/python import syssys.setrecursionlimit(91832)  #set the maximum depth as 10000 Maze=[[1,1,1,1,1,1,1,1,1,1],[0,0,0,1,0,0,0,1,0,1],[1,0,0,1,0,0,0,1,0,1],[1,0,0,0,0,1,1,0,0,1],[1,0,1,1,1,0,0,0,0,1],[1,0,0,0,1,0,0,0,0,1],[1,0,1,0,0,0,1,0,0,1],[1,0,1,1,1,0,1,1,0,1],[1,0,0,0,0,1,0,0,0,0],[1,1,1,1,1,1,1,1,1,1]]used=[[ 0 for i in range(10)] for i in range(10)]  #use用于記錄已走過的坐標(biāo),防止重走,同時(shí)便于回溯dx=[ ]dy=[ ]def movel(i,srex,srey,dstx,dsty)#i表示第幾步,當(dāng)前坐標(biāo)和終點(diǎn)坐標(biāo) print(i, srex, srey, dstx, dsty)   if srex==dstx and srey==dsty:     print(run away!)dx append( srex)    dy append( srey)      return True if maze[srex+1][ srey]==0 and used[ sex+1][srcy]==0: print("down",srcx+1srcy) dx.append( srcx) dy.append( srcy) srcx +=1 used[srcx][srcy]=1 move( i+1,srcx, srcy, dstx, dsty) used[srcx][srcy]=0dx. pop()  dy. pop() if maze[srcx][srcy+1]==0 and used[ srcx][ srcy+1]==0: print("right",srcx, srcy+1) dy.append( srcx) dy.append( srcy)srcy + =1 used[ srcx][srcy]=1 move(i+1,srcx, srcy, dstx, dsty) used[ srcx][srcy]=0 dx. pop(); dy.pop();  if maze[ srcx][ srcy-1]==0 and used[ srcx][ srcy-1==0:    print("left",srcx,srcy+1)dy.append( srcx)dy.append( srcy)    srcy-=1used[ srcx][srcy]=1move( i+1srcx, srcy, dstx, dsty)    dx. pop()    dy. pop() if maze[srcx-1][srcy]==0 and used[srcx-1][srcy]==0:   print("up",srcx, srcy+1)   dx.append(srcx)   dy.append(srcy)   srcx-= 1   used[ srcx][srcy]=1used[ srcx][srcy]=1   move(i+1,srcx, srcy, dstx, dsty)       used[ srcx][srcy]=0   dx. pop()   dy.pop() def main():move(1,1,0,8,9)  #move(1,1,0,8,9)    for i in range(len(dx)):print('The{}step is({},{}).fomat(i+1,dx[i],dy[i])) If _name_=='_main_'main()3.統(tǒng)計(jì)英文短文中某單詞出現(xiàn)的次數(shù) (3)假設(shè)有一篇英文短文,請畫出算法流程圖并編程實(shí)現(xiàn)以下操作:統(tǒng)計(jì)英文短文中某單詞出現(xiàn)的次數(shù)。在英文短文中查找并替換某單詞。參考答案:采用遞歸的方式實(shí)現(xiàn),流程圖如下所示:                                                          參考代碼如下所示:#!/usr/bin/python#coding=utf-8 def main();english_paragraph="Hello,world.This is a new world for you."  #紅色部分是編者假設(shè)的短文內(nèi)容find_word="word"  #紅色部分是編者假設(shè)的查找字符replaced_word="you"   #紅色部分是編者假設(shè)的精換的字符 find(english_paragraph,find_word) #查找字符print("\r\n===========================\r\n)replace(english_paragraph,replaced_word,find_word)  #精換字符 def alphabet_in_control_list(alphabet)control_list=[" ",,,.,"\","\]if alphabet in control_list:return Truereturn False def find(english_paragraph,find_word)WillFind="Total=0for alphabet in english_paragraphif alphabet_in_control_list(alphabet)if WillFind==find_word;Total=Total+1WillFind=””else:WillFind=WillFind+alphabet print("find",find_word,"in paragraph,total:",Total,".") def replace(english_paragraph,replaced_word,find_word)Will Find= “”Replaced Paragraph=""for alphabet in english_paragraph:if alphabet_in_control_list(alphabet):if Will Find==find_word;Replaced Paragraph=Replaced Paragraph+replaced_word+alphabetelse:  Replaced Paragraph=Replaced Paragraph+Will Find+alphabetWill Find=””else:Will Find=Will Find+alphabetprint("replaced result",Replaced Paragraph)if _name_=='_main_':main()   

相關(guān)試卷

高中信息技術(shù)滬教版 (2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)1.問題分析課后復(fù)習(xí)題:

這是一份高中信息技術(shù)滬教版 (2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)1.問題分析課后復(fù)習(xí)題,共60頁。PPT課件主要包含了第三章房間篇,◇物業(yè)服務(wù)等內(nèi)容,歡迎下載使用。

2019CSP-j普及組a卷試題及參考答案:

這是一份2019CSP-j普及組a卷試題及參考答案,共10頁。

高中滬教版(2019)第三單元 網(wǎng)絡(luò)安全基礎(chǔ)本章綜合與測試精品綜合訓(xùn)練題:

這是一份高中滬教版(2019)第三單元 網(wǎng)絡(luò)安全基礎(chǔ)本章綜合與測試精品綜合訓(xùn)練題,共2頁。

英語朗讀寶

相關(guān)試卷 更多

高中信息技術(shù)滬教版(2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第五單元 排序與查找本章綜合與測試優(yōu)秀精練

高中信息技術(shù)滬教版(2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第五單元 排序與查找本章綜合與測試優(yōu)秀精練

滬教版(2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第四單元 二叉樹本章綜合與測試精品課堂檢測

滬教版(2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第四單元 二叉樹本章綜合與測試精品課堂檢測

高中信息技術(shù)滬教版(2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第二單元 初識數(shù)據(jù)結(jié)構(gòu)本章綜合與測試優(yōu)秀當(dāng)堂達(dá)標(biāo)檢測題

高中信息技術(shù)滬教版(2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第二單元 初識數(shù)據(jù)結(jié)構(gòu)本章綜合與測試優(yōu)秀當(dāng)堂達(dá)標(biāo)檢測題

信息技術(shù)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第一單元 走進(jìn)數(shù)據(jù)時(shí)代本章綜合與測試精品練習(xí)

信息技術(shù)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)第一單元 走進(jìn)數(shù)據(jù)時(shí)代本章綜合與測試精品練習(xí)

資料下載及使用幫助
版權(quán)申訴
版權(quán)申訴
若您為此資料的原創(chuàng)作者,認(rèn)為該資料內(nèi)容侵犯了您的知識產(chǎn)權(quán),請掃碼添加我們的相關(guān)工作人員,我們盡可能的保護(hù)您的合法權(quán)益。
入駐教習(xí)網(wǎng),可獲得資源免費(fèi)推廣曝光,還可獲得多重現(xiàn)金獎(jiǎng)勵(lì),申請 精品資源制作, 工作室入駐。
版權(quán)申訴二維碼
高中信息技術(shù)滬教版 (2019)選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)電子課本

本章綜合與測試

版本: 滬教版 (2019)

年級: 選修1 數(shù)據(jù)與數(shù)據(jù)結(jié)構(gòu)

切換課文
所有DOC左下方推薦
歡迎來到教習(xí)網(wǎng)
  • 900萬優(yōu)選資源,讓備課更輕松
  • 600萬優(yōu)選試題,支持自由組卷
  • 高質(zhì)量可編輯,日均更新2000+
  • 百萬教師選擇,專業(yè)更值得信賴
微信掃碼注冊
qrcode
二維碼已過期
刷新

微信掃碼,快速注冊

手機(jī)號注冊
手機(jī)號碼

手機(jī)號格式錯(cuò)誤

手機(jī)驗(yàn)證碼 獲取驗(yàn)證碼

手機(jī)驗(yàn)證碼已經(jīng)成功發(fā)送,5分鐘內(nèi)有效

設(shè)置密碼

6-20個(gè)字符,數(shù)字、字母或符號

注冊即視為同意教習(xí)網(wǎng)「注冊協(xié)議」「隱私條款」
QQ注冊
手機(jī)號注冊
微信注冊

注冊成功

返回
頂部