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

pdf
81.7 KB
4 頁
moex
侵權投訴
加載中. ..
PDF
114年公務人員特種考試關務人員身心障礙人員考試及
114
關務人員考試
四等考試
資訊處理(選試英文)
程式設計概要
試時間:1小時 30 座號:
注意止使
科目除專或數使用本國
14530
4
1
一、下列 C程式,輸入兩個字串,字串長度最長為 10 字元可重複。程式判
斷字串中的每一個字元若兩個字串在同一個位置的字元都相同則記錄
一個 A若兩個字串都有這個字元但位置不同則記錄一個 B式最終
輸出具 AB情況的字元與其數量。請寫出程式執行後輸出,以及計算
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 <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;
}
代號:
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(&apos;.&apos;)
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 = &apos;0123456789abcdef&apos;
newS = &apos;&apos;
parts = s.split(&apos;:&apos;)
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 &apos;abcdef&apos;: newS += e.upper()
else: newS += e
return True, newS
def isC(s):
if s.count(&apos;.&apos;) (I) 1: # (I)
return (II) #(II)
left, right = s.split(&apos;.&apos;)
return left.isdigit() (III) right.isdigit() # (III)
def detect_type(symbol):
if isA(symbol): return &apos;A&apos;
b, bs = isB(symbol)
if b : return &apos;B=>&apos;+ bs
if isC(symbol): return &apos;C&apos;
return &apos;E&apos;
print(detect_type(&apos;140.100.100.80&apos;))
print(detect_type(&apos;140.180.101.81&apos;))
print(detect_type(&apos;f44d:30f8:1694&apos;))
print(detect_type(&apos;TAIWAN&apos;))
print(detect_type(&apos;123.52&apos;), end=&apos;,&apos;) #先註解
print(detect_type(&apos;0.987&apos;)) #先註解
代號:
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()
收藏 ⬇️ 下載