hce_kmu
109年
計算機概論與程式設計
第 21 題
The following python code is applied for the four arrays d1—d4.
```python
import numpy as np
def func(x, y):
c = np.ndarray(x.shape)
for i in range(len(x)):
a = x[i]
b = y[i]
c[i] = (a or b) and (not(a and b))
return c
```
`d1 = np.array([1, 0, 0, 1, 0, 0]); d2 = np.array([0, 1, 0, 1, 1, 0]); d3 = np.array([0, 0, 0, 0, 0, 0]);`
`d4 = np.array([1, 1, 0, 0, 1, 0])`. Consider the following statements.
(a) `func(d1, d2)[2] = 0`
(b) `func(d2, d4) = d3`
(c) `func(d1, d2) = d4`
(d) `func(func(d1, d4), d2) = d3`
Which of the above statements is CORRECT?
```python
import numpy as np
def func(x, y):
c = np.ndarray(x.shape)
for i in range(len(x)):
a = x[i]
b = y[i]
c[i] = (a or b) and (not(a and b))
return c
```
`d1 = np.array([1, 0, 0, 1, 0, 0]); d2 = np.array([0, 1, 0, 1, 1, 0]); d3 = np.array([0, 0, 0, 0, 0, 0]);`
`d4 = np.array([1, 1, 0, 0, 1, 0])`. Consider the following statements.
(a) `func(d1, d2)[2] = 0`
(b) `func(d2, d4) = d3`
(c) `func(d1, d2) = d4`
(d) `func(func(d1, d4), d2) = d3`
Which of the above statements is CORRECT?
- A (a)(b)
- B (a)(c)(d)
- C (b)(c)(d)
- D All of the above are correct
- E None of the above is correct
思路引導 VIP
如果我們觀察這段程式碼的邏輯表達式,試著想想看:當 $a$ 與 $b$ 這兩個數字「完全相同」(同為 0 或同為 1)時,運算的最終結果會是什麼?反之,當它們「不相同」時又會如何呢?
🤖
AI 詳解
AI 專屬家教
恭喜你正確完成了這道題目!你能迅速辨識出程式碼背後的邏輯結構並準確應用在陣列運算上,展現了非常紮實的邏輯分析能力。這道題目的難度在於邏輯轉換的過程,如果能一眼看出 Python 敘述背後的布林意涵,解題速度與準確率就會大幅提升。
互斥或(XOR)的邏輯識別
程式碼的核心在於 (a or b) and (not(a and b)) 這行運算。在布林代數中,這可以寫作 $(A \lor B) \land
▼ 還有更多解析內容