114 關務特考四等 資訊處理(選試英文)科別 程式設計概要 考古題詳解
第 一 題25 分
下列 C 程式,輸入兩個字串,字串長度最長為 10 且字元可重複。程式判斷字串中的每一個字元,若兩個字串在同一個位置的字元都相同,則記錄一個 A;若兩個字串都有這個字元但位置不同,則記錄一個 B。程式最終輸出其 A 與 B 情況的字元與其數量。
請寫出程式執行後輸出,以及計算 、、、 的程式碼之運作邏輯。
#include <stdio.h> void compare_strings(const char *X, const char *Y, int N) { int A = 0, B = 0; int countX[256] = {0}, countY[256] = {0}; char partA[11] = {0}, partB[11] = {0}; int indexA = 0, indexB = 0, partAN[11];
for (int i = 0; i < N; i++) {
if (X[i] == Y[i]) {
partA[indexA++] = X[i];
A++;
} else {
countX[(unsigned char)X[i]]++;
countY[(unsigned char)Y[i]]++;
}
}
for (int i = 0; i < 256; i++) {
int min_count = (countX[i] < countY[i]) ? countX[i] : countY[i];
if (min_count > 0) {
for (int j = 0; j < min_count; j++)
partB[indexB++] = i;
B += min_count;
}
}
printf("%dA%dB; A: %s, B: %s", A, B, partA, partB);
}
int main() { compare_strings("3A5@3", "35A63", 5); compare_strings("f%09#2", "g5029#", 6); return 0; }
登入後即可作答並保存紀錄。
核心觀念
本題考字串逐位比較,以及用頻率統計處理重複字元:
- A:兩字串同一位置的字元相同,記錄該字元並將 加一。
- B:排除已計入 A 的位置後,某字元在兩字串中都出現,且位於不同位置;可配對的數量由兩邊該字元次數的較小值決定。
countX、countY 分別統計未形成 A 的位置中,各字元在 X、Y 出現的次數。partA、partB 則依序保存 A、B 的字元。
解題方法
第一組:"3A5@3" 與 "35A63"
依位置比較:
| 位置 | X | Y | 結果 |
|---|---|---|---|
| 0 | 3 | 3 | 相同,記為 A |
| 1 | A | 5 | 不同,納入字元統計 |
| 2 | 5 | A | 不同,納入字元統計 |
| 3 | @ | 6 | 不同,納入字元統計 |
| 4 | 3 | 3 | 相同,記為 A |
因此 A = 2,partA 依比較順序為 "33"。
未形成 A 的字元中,X 有 A、5、@,Y 有 5、A、6。字元 A 與 5 各在兩邊出現一次,故各形成一個 B;@ 和 6 沒有配對。程式以字元編碼由小到大掃描,因此 partB 排為 "5A",B = 2。
第二組:"f%09#2" 與 "g5029#"
依位置比較:
| 位置 | X | Y | 結果 |
|---|---|---|---|
| 0 | f | g | 不同,納入字元統計 |
| 1 | % | 5 | 不同,納入字元統計 |
| 2 | 0 | 0 | 相同,記為 A |
| 3 | 9 | 2 | 不同,納入字元統計 |
| 4 | # | 9 | 不同,納入字元統計 |
| 5 | 2 | # | 不同,納入字元統計 |
第 二 題25 分
下列 Python 程式,可辨識輸入字串的不同類型。當註解掉 isC(s) 函式程式碼,以及 Line 42、43,請寫出程式執行後的輸出。若未註解上述程式碼,請完成 isC(s) 函式 (I)~(III) 程式空格,辨識輸入 是否為浮點小數,使得 Line 42、43 輸出 C,C。
def isA(s): parts = s.split('.') if len(parts) != 4: return False for part in parts: if not part.isdigit(): return False if int(part) < 0 or int(part) >= 150: return False return True
def isB(s): Elements = '0123456789abcdef' newS = "" parts = s.split(':') if len(parts) != 3: return False, newS for part in parts: if len(part) != 4: return False, newS for c in part: if not (c in Elements): return False, newS
for e in s:
if e in 'abcdef':
newS += e.upper()
else:
newS += e
return True, newS
def isC(s): if s.count('.') ____ 1: # (I) return ____ # (II) left, right = s.split('.') return left.isdigit() ____ right.isdigit() # (III)
def detect_type(symbol): if isA(symbol): return 'A' b, bs = isB(symbol) if b: return 'B=>' + bs if isC(symbol): return 'C' return 'E'
print(detect_type('140.100.100.80')) print(detect_type('140.180.101.81')) print(detect_type('f44d:30f8:1694')) print(detect_type('TAIWAN')) print(detect_type('123.52'), end=';') print(detect_type('0.987'))
登入後即可作答並保存紀錄。
核心觀念
本題考查 Python 字串處理、布林運算與函式回傳值:
split('.')依小數點切分字串;若字串恰有一個小數點,會得到左右兩段。isdigit用來判斷字串是否由數字字元組成;空字串也會回傳False。and只有在左右兩個條件都為True時,整體才為True。isA、isB、isC依序辨識不同格式;detect_type依序呼叫它們,並回傳類型代碼。
解題方法
先依題意註解 isC(s) 與 Line 42、43,因此程式只執行前四個 print 及最後兩個輸出值。逐筆判斷如下:
'140.100.100.80':以.切分成四段,各段都是數字,且都介於 (含)至 (不含)之間,因此isA回傳True,輸出A。'140.180.101.81':雖然切成四段且都是數字,但180 >= 150,所以isA回傳False。它也不是符合isB格式的字串,輸出E。'f44d:30f8:1694':以:切分成三段,每段長度皆為四,且字元都屬於0123456789abcdef,所以isB回傳(True, 'F44D:30F8:1694'),輸出B=>F44D:30F8:1694。'TAIWAN':不符合isA或isB的格式,輸出E。'123.52'與'0.987':原題未註解isC時會判斷為浮點小數;但依題目指定,isC已註解,程式仍會呼叫不存在的函式。
第 三 題25 分
針對下列 Java 程式碼,請寫出此程式所有類別或介面之間的關係,以及執行後之輸出;並請說明 Line 34、39 程式碼有問題的原因。
interface Food { // 食物 public abstract int getWeight(); }
class Deer implements Food { // 鹿 public Deer(int w) { weight = w; }
public int getWeight() {
return weight;
}
private int weight;
}
interface Dragon { // 龍 public abstract int eat(Food food); }
class Dinosaur implements Dragon { // 恐龍 public Dinosaur() { quantity = 0; }
public int eat(Food f) {
quantity += f.getWeight();
return quantity;
}
private int quantity;
}
class Tyrannosaurus extends Dinosaur { // 暴龍 public Tyrannosaurus(Food f) { food = f; }
public String hunt(Food f) {
eat(food);
food = f;
int q = eat(f);
return food.getWeight() + ":" + q;
}
private Food food;
}
public class Hunt { public static void main(String[] args) { Food f1 = new Deer(3); Food f2 = new Deer(5);
// Dragon d1 = new Dragon(); // 程式問題
Dragon d2 = new Dinosaur();
Dragon t1 = new Tyrannosaurus(f1);
Tyrannosaurus t2 = new Tyrannosaurus(f1);
System.out.println("Dinosaur eat: " + d2.eat(f1));
// System.out.println("Tyrannosaurus eat: " + t1.hunt(f2)); // 程式問題
System.out.println("Tyrannosaurus eat: " + t2.hunt(f2));
}
}
登入後即可作答並保存紀錄。
核心觀念
本題考查 Java 的介面實作、類別繼承、多型與物件狀態累積。
- 類別以
implements實作介面,必須提供介面宣告的方法。 - 類別以
extends繼承另一個類別,可沿用父類別的公開方法。 - 變數的宣告型別決定可呼叫哪些方法;物件的實際型別決定執行時使用哪個實作。
- 介面不能直接建立物件;必須建立實作該介面的具體類別物件。
解題方法
先整理類別與介面關係,再依 main 的執行順序追蹤各物件內的 quantity。Dinosaur 建構時將 quantity 設為 0,而 Tyrannosaurus 繼承 Dinosaur,因此每隻暴龍也各自有一個初始為 0 的 quantity。
類別與介面關係
Deer implements Food:Deer實作Food,提供getWeight。Dinosaur implements Dragon:Dinosaur實作Dragon,提供eat(Food)。Tyrannosaurus extends Dinosaur:Tyrannosaurus繼承Dinosaur,可使用繼承而來的eat(Food);它本身未另外宣告implements Dragon,但仍可透過繼承而符合Dragon。Hunt:包含main方法,負責建立物件並呼叫方法。
逐步追蹤輸出
f1指向重量為 3 的Deer;f2指向重量為 5 的Deer。d2指向一個Dinosaur,其quantity初值為 0。t2指向一個Tyrannosaurus,其父類別建構後quantity初值為 0,且food設為f1。
第一行呼叫 d2.eat(f1):將重量 3 加入 Dinosaur 的 quantity,得到 3,因此輸出:
Dinosaur eat: 3
第 四 題25 分
下列 Python 程式實作堆疊抽象資料型別,設定最大容量為 2。程式輸出是:
Push success=True Push success=True Push success=False Pop success=True, data=8 Pop success=True, data=5 Pop success=False, data=-1
請填入 Line 01、03、06、10、18 程式碼空格 (I)~(V),使程式能正確執行。
Line 01:MaxSize = ____ # (I)
Line 03:return top == ____ # (II)
Line 06:return top == MaxSize ____ 1 # (III)
Line 10:top ____ 1 # (IV)
Line 18:top ____ 1 # (V)
def is_empty(top): return top == ____
def is_full(top): return top == MaxSize ____ 1
def push(stack, top, n): if not is_full(top): top ____ 1 stack[top] = n return top, True return top, False
def pop(stack, top): if not is_empty(top): data = stack[top] top ____ 1 return top, data, True return top, -1, False
def main(): stack = [0] * MaxSize top = -1
top, success = push(stack, top, 5)
print(f"Push success={success}")
top, success = push(stack, top, 8)
print(f"Push success={success}")
top, success = push(stack, top, 7)
print(f"Push success={success}")
top, data, success = pop(stack, top)
print(f"Pop success={success}, data={data}")
top, data, success = pop(stack, top)
print(f"Pop success={success}, data={data}")
top, data, success = pop(stack, top)
print(f"Pop success={success}, data={data}")
if name == "main": main()
登入後即可作答並保存紀錄。
核心觀念
本題考查堆疊(Stack)的後進先出(LIFO)特性,以及用索引變數 top 記錄堆疊頂端的位置。
陣列長度為 MaxSize,索引從 開始,因此滿堆疊時的頂端索引是 MaxSize - 1。堆疊為空時,top 設為 ;每次成功推入(push)一筆資料,top 加 ;每次成功彈出(pop)一筆資料,top 減 。
解題方法
依照題目容量為 ,以及空堆疊起始值 top = -1,可逐一填入:
- (I)
MaxSize = 2:設定堆疊最大容量為 。 - (II)
return top == -1:top為 表示堆疊為空。 - (III)
return top == MaxSize - 1:最後一個有效索引為容量減 ,所以top到達該索引時堆疊已滿。 - (IV)
top += 1:推入資料前,先將頂端索引移至下一個空位。