113 高普考普通考試 資訊處理 程式設計概要 考古題詳解

113 年・高普考・普通考試・資訊處理
試題代碼 程式設計概要

第 一 題25 分

請使用 C、C++、C#、Java 或 Python 程式語言,撰寫一個進位制轉換程式的方法。方法接受一個合法的整數,並以參數形式回傳十進位制分別轉成以 string 資料型別表示的二、八及十六進位制結果。

主程式負責持續接受輸入,檢查輸入為合法的整數值後,呼叫轉換程式的方法並顯示結果。如果輸入不是合法的整數,則結束程式的執行。

執行範例如下:

測試一:輸入 31 → 呼叫 tenBaseTo2_8_16base(31),回傳 11111、37、1F。

測試二:輸入 168 → 呼叫 tenBaseTo2_8_16base(127),回傳 10101000、250、A8。

限制一:所撰寫的方法內,不可以使用任何程式語言內建或第三方提供的套件,即 Python/Java 不可以使用 import,C++、C# 不可以使用 using 再加入套件……

限制二:不可使用 recursive 的方式,亦不可以使用 goto。

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查整數進位轉換、迴圈與字串組合。將十進位整數轉成基底 bb 的表示法,可反覆以 bb 除該數,記錄每次的餘數;由於餘數由低位到高位產生,最後要反向排列。

二進位的 b=2b=2,八進位的 b=8b=8;十六進位的 b=16b=16,其中餘數 1010 至 1515 分別表示為 A 至 F。

解題方法

以下以 Python 撰寫,不使用 import、遞迴或 goto。轉換方法以參數接收合法整數,回傳包含二、八、十六進位字串的 tuple。題目範例所列的測試二有誤:輸入為 168,因此呼叫時應傳入 168,不是 127。

🔒

後續完整解題步驟與【答案】

免費註冊,享 3 天全站完整詳解閱覽。

免費註冊

第 二 題25 分

如下的 Python 程式,請說明執行後,如果輸入 "guava",顯示的結果為何?又如果輸入 "kiwi",顯示的結果又為何?如無法正確執行,請說明原因,以及應如何修正。

假設 createLL 方法會正確地建立單向連結的 linked list。

class Node: def init(self, data): self.data = data self.next = None

def linearSearchR(node, target, index=0, callCount=0): callCount += 1 # increment the call count if node is None: return (False, -1, callCount) if node.data == target: return (True, index, callCount) return linearSearchR(node.next, target, index+1, callCount)

def linearSearchL(node, target): current = head index = 0 comparisonCount = 0 while current is not None: comparisonCount += 1 # increment the comparison count if current.data == target: return (True, index, comparisonCount) current = current.next index += 1 return (False, -1, comparisonCount)

if name == "main": strings = ["apple", "banana", "cherry", "guava", "mango", "pineapple"] head = createLL(strings) target = input("Please input the searching target --> ")

found, index, call_count = linearSearchR(head, target)
print(f"\nNumber of recursive calls made --> {call_count}")
if found:
    print(f"{target} found in the linked list at index {index}.")
else:
    print(f"{target} not found in the linked list.")

found, index, comparisonCount = linearSearchL(head, target)
print(f"\nNumber of comparisons made --> {comparisonCount}")
if found:
    print(f"{target} found in the linked list at index {index}.")
else:
    print(f"{target} not found in the linked list.")

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查以遞迴與迴圈在單向連結串列中進行線性搜尋,並比較兩種寫法如何計算搜尋次數。

串列內容依序為:

apple → banana → cherry → guava → mango → pineapple

索引從 00 開始,因此 "guava" 位於索引 33。

解題方法

遞迴版 linearSearchR 每進入一次函式,就先將 callCount 加 11。找到目標時立即回傳;找不到時,還會在 node 變成 None 時多進入一次函式。因此,成功搜尋的呼叫次數等於目標索引加 11;搜尋失敗時,呼叫次數等於節點數加 11。

迴圈版 linearSearchL 每比較一個節點,才將 comparisonCount 加 11。找到目標便回傳;若走完整個串列仍未找到,則回傳比較過的節點數。

不過題目程式有一處錯誤:linearSearchL(node, target) 雖然收到 node 參數,函式內卻寫成 current = head。在題目所示主程式中,head 是全域變數,因此搜尋仍會從串列頭開始;但這使函式忽略傳入的 node,不適合作為一般函式使用。應修正為:

def linearSearchL(node, target):
 current = node

輸入 "guava" 的結果

遞迴搜尋依序檢查 apple、banana、cherry、guava,共呼叫 44 次,找到索引 33。

🔒

後續完整解題步驟與【答案】

免費註冊,享 3 天全站完整詳解閱覽。

免費註冊

第 三 題30 分

請就如下的 Java 程式,說明程式中各方法(包括建構子)執行的順序,以及最後顯示的結果。

註:執行程式說明,如 class LinkedList 建構子 (head, tail) → ……

class Node { int value; Node next;

public Node(int vv, Node nn) {
    this.value = vv;
    this.next = nn;
    System.out.println("class Node \tconstructor \tvalue " + vv);
}

}

class LinkedList { protected Node head; protected Node tail;

public LinkedList() {
    this.head = null;
    this.tail = null;
    System.out.println("class LL \tconstructor()");
}

public LinkedList(Node head, Node tail) {
    this.head = head;
    this.tail = tail;
    System.out.println("class LL \tconstructor(head, tail)");
}

public void addToHead(Node newNode) {
    if (head == null) {
        head = newNode;
        tail = newNode;
    } else {
        newNode.next = head;
        head = newNode;
    }
    System.out.println("class LL \tmethod \taddToHead " + newNode.value);
}

public void addToTail(Node newNode) {
    if (tail == null) {
        head = newNode;
        tail = newNode;
    } else {
        tail.next = newNode;
        tail = newNode;
    }
    System.out.println("class LL \tmethod \taddToTail " + newNode.value);
}

}

class SinglyLinkedList extends LinkedList { public SinglyLinkedList(Node head, Node tail) { super(head, tail); System.out.println("class SLL \tconstructor(head, tail)"); } }

public class PQ3_LL { public static void main(String[] args) { int[] data = {90, 50, 70, 30, 40}; SinglyLinkedList sllObj = new SinglyLinkedList(null, null);

    for (int i = 0; i < data.length; i++) {
        if (i % 2 == 0) {
            sllObj.addToHead(new Node(data[i], null));
        } else {
            sllObj.addToTail(new Node(data[i], null));
        }
    }
}

} // End of main class

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查 Java 的物件建構順序、子類別呼叫父類別建構子,以及單向鏈結串列的首尾指標更新方式。

建立子類別物件時,會先執行父類別建構子;super(head, tail) 明確呼叫 LinkedList(Node, Node)。本題傳入的兩個參數都是 null,因此串列一開始為空。

解題方法

依程式執行順序追蹤:先建立 SinglyLinkedList,接著逐一處理陣列元素。每次迴圈會先建立 Node,再依 i 的奇偶性呼叫 addToHead 或 addToTail。

idata[i]呼叫方法操作後串列(由 head 到 tail)
090addToHead90
150addToTail90 → 50
270addToHead70 → 90 → 50
330addToTail70 → 90 → 50 → 30
440addToHead40 → 70 → 90 → 50 → 30

逐步看各次操作:

  • i = 0:串列為空,addToHead 將 head、tail 都設為值為 90 的節點。
  • i = 1:addToTail 將原尾節點 90 的 next 指向 50,再把 tail 更新為 50。
  • i = 2:addToHead 將新節點 70 的 next 指向原 head 90,再把 head 更新為 70。
🔒

後續完整解題步驟與【答案】

免費註冊,享 3 天全站完整詳解閱覽。

免費註冊

第 四 題20 分

請撰寫 HTML 程式,執行後可以呈現如下的頁面,其中「Year」是最多四位數的整數值。按下 Submit 按鈕後,以 POST 形式傳送表單的資料到後端「addMovie.php」處理。

🖼️【此處有附圖,請對照原卷】

頁面內容如下:

Add Movie

Name of Movie:文字輸入欄位

Year:整數輸入欄位,畫面預設值為 2024,最多四位數。

Movie Genre:下拉式選單,選項如下:

(A) Action (B) Comedy (C) Drama (D) Horror (E) Fantasy (F) Romance (G) Science Fiction

Rating:單選按鈕,選項如下:

🖼️ 本題原卷含圖表。基於試題來源之版權考量,本站不轉載原卷影像; 題目文字已完整轉錄,圖表請自行參閱考選部公告之試題原檔。
這一題的完整詳解

核心觀念

本題考查 HTML 表單的基本結構與控制項:

  • 使用 <form> 建立表單。
  • method="post" 表示以 POST 方式傳送資料。
  • action="addMovie.php" 指定後端處理程式。
  • 文字欄位使用 <input type="text">。
  • 年份使用 <input type="number">,以 min、max 限制整數範圍。
  • 電影類型使用 <select> 與 <option>。
  • 評分使用多個具有相同 name 的 <input type="radio">,確保只能選一項。
  • 每個控制項都要設定 name,後端 PHP 才能取得 POST 資料。

解題方法

依照畫面內容建立表單:

  1. 電影名稱設定為文字輸入欄位。
  2. 年份預設值為 2024,最多四位數,因此設定範圍為 00 至 99999999。
  3. 電影類型建立七個下拉式選項,預設選取 Action。
  4. 評分建立 Kids、PG13、Adult 三個單選按鈕,預設選取 Adult。
  5. Submit 按鈕放在表單內,按下後將資料以 POST 傳至 addMovie.php。

HTML 程式

🔒

後續完整解題步驟與【答案】

免費註冊,享 3 天全站完整詳解閱覽。

免費註冊

其他考古題