免費詳解 / 中正 / 軟體 / 103

103 中正資工所考古題解答|軟體逐題詳解

103 中正資工所軟體考古題的逐題詳解,共 12 題,免費公開。

逐題詳解

120%

Multiple-choice questions.(單選題)

1.1. (5%) Consider the following C++ code.

#include <iostream>
using namespace std;

class Copied {
public:
  int* num;
  int len;

  Copied (int length) {
    num = new int[100];
    for (int i = 0; i < 100; i++) num[i] = i;
    len = 100;
  }

  Copied (const Copied& c) : // Line A
    num(c.num), len(c.len) { }

  virtual Copied operator=(const Copied& c) {return Copied(c);}
};

int main( ) {
  Copied n1(5);
  Copied n2(n1);
  n2.num[0] = 50;
  cout << "n2.num[0] = " << n2.num[0];
  cout << ", n1.num[0] = " << n1.num[0];
  cout << endl;
}

What is printed by the cout statements in main? (a) n2.num[0] =50, n1.num[0] = 50 (b) n2.num[0] = 50, n1.num[0] = 0 (c) n2.num[0] = 0, n1.num[0] = 50 (d) n2.num[0] = 0, n1.num[0] = 0 (e) There is an error because << is not overloaded by the copied class.

1.2. (5%) Continue the problem 1.3. Replace the constructor Copied (const Copied& c) at (Line A) above with the code below. With this new constructor, what is printed by the cout statement?

Copied (const Copied& c) : len(c.len) {
  num = new int[len];
  for (int i = 0; i < len; i++) { num[i] = c.num[i]; }
}

(a) n2.num[0] = 50, n1.num[0] = 50 (b) n2.num[0] = 50, n1.num[0] = 0 (c) n2.num[0] = 0, n1.num[0] = 50 (d) n2.num[0] = 0, n1.num[0] = 0 (e) n2.num[0] = 50, n1.num[0] = 50 (f) There is an error because << is not overloaded by the copied class.

1.3. (5%) A function prototype does not have to ______. (a) include argument names (b) end with a semi-colon (c) agree with the function header (d) match with all calls of the function

1.4. (5%) In C++, to exhibit polymorphism it is necessary to ______. (a) declare all methods that will exhibit polymorphic behavior as virtual (b) access the methods by a pointer or reference (c) declare the class as abstract (d) declare the class as polymorphic (e) (a) and (b) (f) (a) and (c) (g) (a) and (e)

1.1 Line A 的 copy constructor 只複製指標 num(c.num),是 shallow copy;n2.num 與 n1.num 指向同一塊陣列,n2.num[0]=50 也改到 n1.num[0] → 兩個都印 50 → (a)

1.2 新 copy constructor 先 new int[len] 再逐一複製,是 deep copy;n2 有自己的陣列,改 n2.num[0] 不影響 n1,n1.num[0] 仍是建構時的 0 → (b)

cout 印的是 int,不需要為 Copied 重載 <<;1.2 的 (a)(e) 內容相同,是共用陣列時的結果

1.3 prototype 只需要參數型別,名稱可省(int f(int, double););它是宣告,必須以分號結尾;必須和 function header 一致,每次呼叫也要和它相容 → (a)

1.4 要 dynamic binding:(a) 方法要宣告成 virtual;(b) 要經由 base class 的 pointer 或 reference 呼叫(直接用物件呼叫在編譯期就綁定了);不一定要 abstract class,C++ 也沒有「宣告 class 為 polymorphic」的語法 → (a) 且 (b) = (e)

1.1 (a) 1.2 (b) 1.3 (a) 1.4 (e)

210%

The GNU C compiler supports an extension to ANSI C called "statement expressions," which allows any compound statement to be treated an expression by enclosing it in parentheses. For example, we can write

#define maxint (a, b) ({int a1 = (a), b1 = (b); a1>b1 ? a1 : b1;})

Compare this to the more usual C definition

#define max (a, b) ((a) > (b) ? (a) : (b))

Consider passing expressions max(i++, j++) and maxint(i++, j++), where these two macros behave differently, which one is probably a better definition, and explain why.

max(i++, j++) 展開成 ((i++) > (j++) ? (i++) : (j++))

比較時 i、j 各加一次,被選中的那個又加一次,回傳的是加過一次後的值

例:i=1, j=2 → 比較 1 > 2 不成立(此時 i=2、j=3),再執行 j++ 回傳 3,結束時 j=4;正確的 max 應該是 2

maxint(i++, j++) 先把兩個參數各算一次存進 a1、b1,再比較

同樣 i=1, j=2:回傳 2,結束時 i=2、j=3,和呼叫一般函式的行為一樣

macro 的參數展開後可能被求值多次,參數有 side effect 就會錯;maxint 每個參數只求值一次,所以是比較好的定義

代價:maxint 只適用 int,而且 statement expression 是 GNU 擴充,不是標準 C

maxint 比較好(參數只求值一次,沒有重複 side effect)

36%

The following C function converts a string of lowercase characters into uppercase characters. For example, it will convert "abc" into "ABC". The parameter str is the string. Fill in the missing expressions in (1) and (2).

void lower2upper(char str[]) {
   int i = 0;
   while (   (1)   ) {
      str[i] = str[i] + (2) ;
      i++;
   }
}

(1) str[i] != '\0'(走到字串結尾就停)

(2) 'A' - 'a'(= −32;ASCII 中大寫比對應的小寫少 32)

(1) str[i] != '\0' (2) 'A' - 'a'

48%

The following C function transposes a \(3\times3\) array of integers. For example, it will transpose {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} into {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}}. The parameter m is the array. Fill in the missing expressions in (1) ~ (8).

void transpose(int (*m)[3]) {
   int i, j, t;
   for (i = (1) ; i < (2) ; i++) {
      for (j = (3) ; j < (4) ; j++) {
         t = m[i][j];
         m[(5)][(6)] = m[(7)][(8)];
         m[j][i] = t;
      }
   }
}

只對 upper triangle(\(j>i\))的每個元素和它的對稱位置交換一次:

(1)(2)(3)(4)(5)(6)(7)(8)
03i+13ijji

t = m[i][j]; m[i][j] = m[j][i]; m[j][i] = t;

(3) 一定要是 i+1:若 j 從 0 開始,每一對會被交換兩次、又換回原樣;(2) 寫 2 也對,因為 i=2 時內層迴圈不會執行

(1) 0 (2) 3 (3) i+1 (4) 3 (5) i (6) j (7) j (8) i

56%

Complete the following C function that sums the elements of a one-dimensional array of integers. The parameter m is the array and the parameter n is the size of the array. You need to use the pointer notation to access the array.

int sum(int *m, int n) { ... }
int sum(int *m, int n) {
    int s = 0;
    for (int *p = m; p < m + n; p++)
        s += *p;
    return s;
}

全程用 *p、p++ 存取,不出現 m[i]

65%

Suppose that the postorder traversal of a binary tree \(T\) is A D B C F E and the inorder traversal of \(T\) is A D B E F C. What is the preorder traversal of \(T\)?

postorder 最後一個 \(E\) 是 root;inorder 中 \(E\) 左邊 \(\{A,D,B\}\) 是 left subtree,右邊 \(\{F,C\}\) 是 right subtree

left:postorder \(A\,D\,B\),root \(B\);inorder \(A\,D\,B\),\(B\) 沒有 right child,\(\{A,D\}\) 在左

\(\{A,D\}\):postorder \(A\,D\),root \(D\);inorder \(A\,D\) → \(A\) 是 \(D\) 的 left child

right:postorder \(C\,F\),root \(F\);inorder \(F\,C\) → \(C\) 是 \(F\) 的 right child

ADBEFC

preorder(根→左→右):\(E,B,D,A,F,C\)

E B D A F C

77%

Quicksort: (a) (5%) Please write a C function to do quicksort on an array of integers. (b) (1%) What is the worst case time complexity of quicksort algorithm? (c) (1%) Please describe a method to avoid the worst case.

(a) Lomuto partition,pivot 取最右邊;呼叫 quicksort(a, 0, n-1)

void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }

int partition(int a[], int lo, int hi) {
    int pivot = a[hi], i = lo - 1;
    for (int j = lo; j < hi; j++)
        if (a[j] <= pivot)
            swap(&a[++i], &a[j]);
    swap(&a[i + 1], &a[hi]);
    return i + 1;
}

void quicksort(int a[], int lo, int hi) {
    if (lo < hi) {
        int p = partition(a, lo, hi);
        quicksort(a, lo, p - 1);
        quicksort(a, p + 1, hi);
    }
}

(b) \(O(n^2)\):每次 pivot 都是最大或最小(例如輸入已排序),\(T(n)=T(n-1)+cn\)

(c) pivot 用 randomized(隨機挑一個元素與 a[hi] 交換再 partition)或 median-of-three;要保證 worst case 也是 \(O(n\log n)\) 則用 median of medians 選 pivot

(b) \(O(n^2)\) (c) randomized pivot/median-of-three

88%

Please write a C function to search on a Binary Search Tree. Each node of the tree has a field named 'key' which is a string with at most 32 bytes. The return value is a pointer to the matched node if there is a match. The return value is NULL if there is no match.

#include <string.h>

struct node {
    char key[33];            /* 最多 32 個字元加上 '\0' */
    struct node *left, *right;
};

struct node *bst_search(struct node *root, const char *key) {
    while (root != NULL) {
        int c = strcmp(key, root->key);
        if (c == 0) return root;
        root = (c < 0) ? root->left : root->right;
    }
    return NULL;
}

字串不能用 == 比較,要用 strcmp:小於 0 往左、大於 0 往右、等於 0 就找到

97%

Bellman-Ford and Dijkstra algorithms have been well-known for finding the Single-Source Shortest paths in a graph. Briefly answer the following questions.

(a) (2%) Which algorithm(s) can be applied on graphs with negative edge? (b) (2%) Which algorithm(s) is able to detect negative cycle? (c) (3%) Given a directed acyclic graph, is there any way to run faster than the above two algorithms?

(a) Bellman-Ford。Dijkstra 假設一個點被取出後距離不會再變小,有負邊時這個假設不成立,可能算錯

(b) Bellman-Ford。做完 \(|V|-1\) 輪 relax 後再做第 \(|V|\) 輪,若還有邊能 relax,就有從 source 走得到的 negative cycle

(c) 有,\(O(V+E)\):先 topological sort,再依 topological order 對每個點的出邊各 relax 一次

DAG 沒有 cycle,照這個順序處理到某點時,所有可能進入它的路徑都已經算過,負權重也沒關係

(a) Bellman-Ford (b) Bellman-Ford (c) 有:topological order 上 relax,\(O(V+E)\)

108%

All-Pairs Shortest Paths

1234231-212

(a) (4%) Compute the lengths of all-pairs shortest paths in this graph using the Floyd-Warshall algorithm. (b) (4%) Briefly describe the transformation process in the Johnson's algorithm which converts the negative edge weights into positive weights without affecting the shortest path solution between any two nodes.

邊:1→2 (2)、1→3 (3)、2→4 (1)、3→2 (−2)、3→4 (1)、4→1 (2)

(a) 列=起點、行=終點,每一輪 \(D^{(k)}[i][j]=\min(D^{(k-1)}[i][j],\ D^{(k-1)}[i][k]+D^{(k-1)}[k][j])\)

\(D^{(0)}\)1234
1023∞
2∞0∞1
3∞−201
42∞∞0

\(k=1\)(經過 1):只有 4 能走到 1:\(d(4,2)=2+2=4\)、\(d(4,3)=2+3=5\)

\(D^{(1)}\)1234
1023∞
2∞0∞1
3∞−201
42450

\(k=2\)(經過 2):\(d(1,4)=2+1=3\)、\(d(3,4)=-2+1=-1\)

\(D^{(2)}\)1234
10233
2∞0∞1
3∞−20−1
42450

\(k=3\)(經過 3):\(d(1,2)=3-2=1\)、\(d(1,4)=3-1=2\)、\(d(4,2)=5-2=3\)

\(D^{(3)}\)1234
10132
2∞0∞1
3∞−20−1
42350

\(k=4\)(經過 4):\(d(2,1)=1+2=3\)、\(d(2,3)=1+5=6\)、\(d(3,1)=-1+2=1\);第 1 列沒有更小的

\(D^{(4)}\)1234
10132
23061
31−20−1
42350

對角線全是 0,沒有 negative cycle

(b) Johnson's algorithm 的 reweighting:

① 加一個新點 \(s\),從 \(s\) 連到每個點一條權重 0 的邊

② 從 \(s\) 跑 Bellman-Ford,得 \(h(v)=\delta(s,v)\);若偵測到 negative cycle 就停止

③ 每條邊重新給權重 \(\hat w(u,v)=w(u,v)+h(u)-h(v)\) ;由 triangle inequality \(h(v)\le h(u)+w(u,v)\),所以 \(\hat w\ge0\)

④ 任一條 \(u\) 到 \(v\) 的路徑,新長度 = 原長度 \(+h(u)-h(v)\),中間的 \(h\) 全部抵消,所有 \(u\to v\) 路徑都加同一個常數,最短路徑不變

⑤ 對每個點跑 Dijkstra 得 \(\hat\delta(u,v)\),還原 \(\delta(u,v)=\hat\delta(u,v)-h(u)+h(v)\)

(a) 最終 \(D^{(4)}\)(如上表);(b) 加 0 權重的新源點 → Bellman-Ford 求 \(h\) → \(\hat w=w+h(u)-h(v)\)

115%

Consider the following procedure for sorting an array of numbers. Give a loop invariant which can be used to prove its correctness.

procedure Sort(A)
1.  for j=2 to length(A)
2.      do key=A[j]
3.         i=j-1
4.         while i>0 and A[i]>key
5.             do A[i+1]=A[i]
6.                i--
7.         A[i+1]=key
8.  return

loop invariant:每次第 1 行的 for 迴圈以索引 \(j\) 開始時,\(A[1..j-1]\) 裡就是原本 \(A[1..j-1]\) 的那些元素,而且已經由小到大排好

initialization:\(j=2\) 時 \(A[1..1]\) 只有一個元素,當然已排好

maintenance:while 迴圈把 \(A[1..j-1]\) 中比 key 大的元素各往右移一格,再把 key 放進空出的位置,\(A[1..j]\) 變成排好的;\(j\) 加 1 後 invariant 仍成立

termination:迴圈結束時 \(j=n+1\),代入得 \(A[1..n]\) 就是原本元素排好的結果 得證

1210%

Consider a chain of \(n\) matrix \(A_1,A_2,\dots,A_n\), where the dimension of \(A_i\) is \(d_{i-1}\times d_i\). Assume the number of multiplications to compute \(A_i\times A_{i+1}\) is \(d_{i-1}\cdot d_i\cdot d_{i+1}\). Let \(m[i,j]\) denote the minimum number of multiplications to compute the product of the subchain \(A_i\times A_{i+1}\times\cdots\times A_j\).

(a) (5%) Give the recursive formula for \(m[i,j]\). (b) (5%) Show, step by step, how to use your recursive formula to compute \(m[1,6]\) from the following tables for computing \(m[i,j]\).

matrix\(A_1\)\(A_2\)\(A_3\)\(A_4\)\(A_5\)\(A_6\)
dimension30×3535×1515×55×1010×2020×25
\(m[i,j]\)j=123456
i=1015,7507,8759,37511,875?
2—02,6254,3757,12510,500
3——07502,5005,375
4———01,0003,500
5————05,000
6—————0

(a) 在 \(A_k\) 與 \(A_{k+1}\) 之間切開:左半 \(d_{i-1}\times d_k\),右半 \(d_k\times d_j\),最後再乘一次

\(m[i,j]=\begin{cases}0 & i=j\\[2pt]\displaystyle\min_{i\le k\lt j}\bigl\{m[i,k]+m[k+1,j]+d_{i-1}d_kd_j\bigr\} & i\lt j\end{cases}\)

(b) \(d_0,\dots,d_6=30,35,15,5,10,20,25\);\(m[1,6]=\min_{1\le k\le5}\{m[1,k]+m[k+1,6]+30\cdot d_k\cdot25\}\),各項查表:

\(k\)\(m[1,k]\)\(m[k+1,6]\)\(d_0d_kd_6\)合計
1010,50030·35·25 = 26,25036,750
215,7505,37530·15·25 = 11,25032,375
37,8753,50030·5·25 = 3,75015,125
49,3755,00030·10·25 = 7,50021,875
511,875030·20·25 = 15,00026,875

最小是 \(k=3\):\(m[1,6]=15{,}125\)

往下查 \(m[1,3]\)、\(m[4,6]\) 的最佳切點,完整括號是 \((A_1(A_2A_3))((A_4A_5)A_6)\)

\(m[1,6]=15{,}125\)(\(k=3\) 切開)

109–115 的詳解也是這樣寫的。

國立中正大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 351 頁,逐題推導。

購買 · NT$ 850 看中正其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com