114年 關務特考 四等 資訊處理 程式設計概要 試卷

pdf
81.7 KB
4 頁
moex
侵權投訴
加載中. ..
PDF
114年公務人員特種考試關務人員、身心障礙人員考試及
114年國 軍 上 校 以 上軍 官 轉 任 公 務人 員 考 試 試 題
考 試 別
:
關務人員考試
等 別
:
四等考試
類 科
:
資訊處理(選試英文)
科 目
:
程式設計概要
考試時間:1小時 30 分 座號:
※注意:禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號
:
14530
頁次
:
4
-
1
一、下列 C程式,輸入兩個字串,字串長度最長為 10 且字元可重複。程式判
斷字串中的每一個字元,若兩個字串在同一個位置的字元都相同,則記錄
一個 A,若兩個字串都有這個字元但位置不同,則記錄一個 B。程式最終
輸出具 A與B情況的字元與其數量。請寫出程式執行後輸出,以及計算
A,B, partA, partB 的程式碼之運作邏輯。(25 分)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include
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;
}
代號:
14530
頁次:
4
-
2
二、下列 Python 程式,可辨識輸入字串的不同類型。當註解掉 isC(s)函式程式
碼,以及 Line 42, 43,請寫出程式執行後的輸出。若未註解上述程式碼,
請完成 isC(s)函式(I)~(III)程式空格,辨識輸入 s是否為浮點小數,使得 Line
42, 43 輸出"C,C"。(25 分)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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('.') (I) 1: # (I)
return (II) #(II)
left, right = s.split('.')
return left.isdigit() (III) 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')) #先註解
代號:
14530
頁次:
4
-
3
三、針對下列 Java 程式碼,請寫出此程式所有類別或介面之間的關係,以及
執行後之輸出;並請說明 Line 34, 39 程式碼有問題的原因。(25 分)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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));
}
}
代號:
14530
頁次:
4
-
4
四、下列 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),使程式能正確執行。
(25 分)
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
MaxSize = (I) # (I)
def is_empty(top):
return top == (II) # (II)
def is_full(top):
return top == MaxSize (III) # (III)
def push(stack, top, n):
if not is_full(top):
top (IV) 1# (IV)
stack[top] = n
return top, True
return top, False
def pop(stack, top):
if not is_empty(top):
data = stack[top]
top (V) 1# (V)
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()
收藏 ⬇️ 下載