21若執行以下的 Python 程式碼,則 func()方法會被呼叫幾次?
(A)6
(B)7
(C)8
(D)9
答案:登入後查看
統計: A(205), B(175), C(38), D(15), E(0) #3036539
統計: A(205), B(175), C(38), D(15), E(0) #3036539
詳解 (共 2 筆)
#6070207
這是 遞迴呼叫:
當你 run 的時候:
開始遞迴:
( 呼叫 1 ) 5>=0 >> 5* func(4) -> 不確定值。
( 呼叫 2 ) 4>=0 >> 4*func (3) - > 不確定值。
( 呼叫 3 ) 3>=0 >> 3*func (2) -> 不確定值。
( 呼叫4) 2>=0 >> 2* func (1) -> 不確定值。
( 呼叫5 ) 1>=0 >> 1* func (0) -> 不確定值。
( 呼叫6 ) 0>=0 >> 0* func ( -1) -> 不確定值。
( 呼叫7 ) -1 沒有>=0 >> esle return 1
找到 func(-1) 的值,並確定了,回傳到上
1層。不斷往上去。
,print ( func(number ))
在 consultant ( 終端機) 上,print 是0。
當你 run 的時候:
開始遞迴:
( 呼叫 1 ) 5>=0 >> 5* func(4) -> 不確定值。
( 呼叫 2 ) 4>=0 >> 4*func (3) - > 不確定值。
( 呼叫 3 ) 3>=0 >> 3*func (2) -> 不確定值。
( 呼叫4) 2>=0 >> 2* func (1) -> 不確定值。
( 呼叫5 ) 1>=0 >> 1* func (0) -> 不確定值。
( 呼叫6 ) 0>=0 >> 0* func ( -1) -> 不確定值。
( 呼叫7 ) -1 沒有>=0 >> esle return 1
找到 func(-1) 的值,並確定了,回傳到上
1層。不斷往上去。
,print ( func(number ))
在 consultant ( 終端機) 上,print 是0。
ㅤㅤ
ㅤㅤ
補充參考, 【 ( 呼叫、判斷、停止、回傳。 )搜尋的次數、達到停止條件、呼叫自己、執行的先後順序之間的關係。】
113年普考-程式設計第二大題。
recursive_搜尋 和 linear_搜尋 ;
的 比較次數 、鏈結串列 問題。
如下的 Python 程式,請說明執行後,
如果輸入"guava"顯示的結果為何?
又如果輸入"kiwi"顯示的結果又為何?如無法正確執行,請說明原因,及應如何修正?(25 分)
因為 scope 作用域的問題,在 linearsearch_L中:
修改的地方: current = head ;
應改正成: current = node。
( 因為head和 node的 scope 作用域 完全不同,才能正確執行。 )
正確執行後的顯示: "guava"有在 strings 中,當執行到guava時
linearsearch_L: 先判斷有無節點-> 比較 ->增加次數-->增加index;
比較成功,回傳,不增加次數,不增加index。
(a is not None --> -->comparisonCount==1 --> 不同 --> index==1
(b is not None -->comparisonCount==2 --> 不同 --> index==2
(c is not None --> comparisonCount==3 --> 不同-->index == 3( 打印 : 3)
(g is not None --> comparisonCount==4 ( 打印: 4 ) --> 同 (return )
linearsearch_R: 先增加次數-->判斷有無節點--> 比較--->增加index;
callCount==1 --> (a is not None --> 不同 --> index==1
callCount==2 --> (b is not None --> 不同 --> index==2
callCount==3 --> (c is not None --> 不同 --> index==3( 打印 : 3)
callCount==4 ( 打印: 4 ) --> (g is ) return
"kiwi"顯示:
linearsearch_R: 先增加次數-->判斷有無節點--> 比較--->增加index;
callCount==1 --> (a is not None --> 不同 --> index==1
callCount==2 --> (b is not None --> 不同 --> index==2
callCount==3 --> (c is not None --> 不同 --> index==3
callCount==4--> (g is not None --> 不同 --> index==4
callCount==5 --> (m is not None --> 不同 --> index==5
callCount==6 --> (p is not None --> 不同 --> index==6
callCount==7 --> ( 打印: 7 ) --> ( return: index: -1 )
linearsearch_L: 先判斷有無節點-> 比較 ->增加次數-->增加index;
(a is not None --> -->comparisonCount==1 --> 不同 --> index==1
(b is not None -->comparisonCount==2 --> 不同 --> index==2
(c is not None --> comparisonCount==3 --> 不同-->index == 3
(g is not None --> comparisonCount==4 --> 不同 --> index==4
(m is not None --> comparisonCount==5 --> 不同-->index == 5
(p is not None --> comparisonCount==6( 打印: 6 ) --> 不同-->index == 6
k is None--> ( return: index: -1 )
=======================
因為 scope 作用域的問題,在 linearsearch_L中:
修改的地方: current = head ;
應改正成: current = node。
( 因為head和 node的 scope 作用域 完全不同,才能正確執行。 )
正確執行後的顯示: "guava"有在 strings 中,當執行到guava時
linearsearch_L: 先判斷有無節點-> 比較 ->增加次數-->增加index;
比較成功,回傳,不增加次數,不增加index。
(a is not None --> -->comparisonCount==1 --> 不同 --> index==1
(b is not None -->comparisonCount==2 --> 不同 --> index==2
(c is not None --> comparisonCount==3 --> 不同-->index == 3( 打印 : 3)
(g is not None --> comparisonCount==4 ( 打印: 4 ) --> 同 (return )
linearsearch_R: 先增加次數-->判斷有無節點--> 比較--->增加index;
callCount==1 --> (a is not None --> 不同 --> index==1
callCount==2 --> (b is not None --> 不同 --> index==2
callCount==3 --> (c is not None --> 不同 --> index==3( 打印 : 3)
callCount==4 ( 打印: 4 ) --> (g is ) return
"kiwi"顯示:
linearsearch_R: 先增加次數-->判斷有無節點--> 比較--->增加index;
callCount==1 --> (a is not None --> 不同 --> index==1
callCount==2 --> (b is not None --> 不同 --> index==2
callCount==3 --> (c is not None --> 不同 --> index==3
callCount==4--> (g is not None --> 不同 --> index==4
callCount==5 --> (m is not None --> 不同 --> index==5
callCount==6 --> (p is not None --> 不同 --> index==6
callCount==7 --> ( 打印: 7 ) --> ( return: index: -1 )
linearsearch_L: 先判斷有無節點-> 比較 ->增加次數-->增加index;
(a is not None --> -->comparisonCount==1 --> 不同 --> index==1
(b is not None -->comparisonCount==2 --> 不同 --> index==2
(c is not None --> comparisonCount==3 --> 不同-->index == 3
(g is not None --> comparisonCount==4 --> 不同 --> index==4
(m is not None --> comparisonCount==5 --> 不同-->index == 5
(p is not None --> comparisonCount==6( 打印: 6 ) --> 不同-->index == 6
k is None--> ( return: index: -1 )
=======================
ㅤㅤ
4
0