Transcript 下載/瀏覽
計算機概論-CH5 演算法 複習問題解答-11~24 解答與說明 第11題解答: 假設N為正整數 X←1 while (X ≤ N) do ( if ( (N mod X) = 0 ) then output X XX + 1 ) /* ‘N mod X’之結果為N除以X之餘數*/ 第13題解答: 虛擬碼是正規程式語言的簡 化版本用來描述演算法。 正規程式語言之撰寫需遵守 嚴格程式語言規則 第14題解答:自行在講義或課本中找答案 解答與說明 第15題解答: X Y Z +YWY ZY ZW 1. 進位Z一定為1 (getting a foot in the door) 2. 兩數之百位數相加 X + Y = ZY=1Y 所以X會等於9 , 且兩數之十位數相加(Y + W)會有進位 3. Y + W =1Z,所以Y+W=11。 4. Z + Y =W, 1 + Y =W, => W=6, Y=5 第16題解答: 1. X *Y=Y, X*X=X, => X=1 (getting a foot in the door) 2. XY * Y = 1Y * Y = YZ, 所以(Y,Z)只能為(2,4)或(3,9) 3. 但因百位數字Y與W不同,因此X+Z會進位,所以 Y應為3,Z為9,V=0, W=4 解答與說明 第17題解答:請說明(課堂討論) 第18題解答: 1. 首先(Andrews, Blake)過通道 2. Andrews 帶燈回去 3. (Johnson, Kelly)過通道 4. Blake帶燈回去 5. 最後(Andrews, Blake)過通道 => 2 minutes => 1 minute => 8 minutes => 2 minutes => 2 minutes 解答與說明 第21題解答:(解答之一) procedure SubStringSearch(FirstString, SecondString) P ← 0; Success ← false; while ((P + length of FirstString) <= (length of SecondString) and Success = false) do [N ← 1; while (P + Nth character in SecondString = Nth character in N FirstString) do FirstString ( N ← N + 1; t i g e r if (N = length of FirstString) g e r v o c SecondString a c t i ... then (Success ← true) ) P ← P + 1; ] P P+N 解答與說明 第22題解答: 迴圈本體:do下面之所有虛擬碼 初設步驟:Last 0; Current 1; 修改步驟:Current Last + Temp; 終止測試步驟: (Current < 100) 輸出數列: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 第23題解答:22題之遞迴版本 procedure MysteryWrite (Last, Current) 重覆呼叫MysteryWrite程序並將 if (Current < 100) then (Current, Temp)值傳遞到 (print the value assigned to Current; MysteryWrite (Last, Current),直 Temp Current + Last; apply MysteryWrite to the values Current and Temp) 到(Current < 100)不成立為止 輸出數列: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 解答與說明 第24題解答: 將print the value assigned to Current移 到最下面敘述