NTU-Exam 板


LINE

課程名稱︰資料結構與演算法 課程性質︰必修 課程教師:林軒田 開課學院:電資學院 開課系所︰資工系 考試日期(年月日)︰2012.04.24 考試時限(分鐘):180分鐘 試題 : Midterm Examination Problem Sheet TIME: 04/24/2012, 14:20-17:20 This is an open-book exam. You can use any printed materials as your reference during the exam. Any electronic devices are not allowed. Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts. Both English and Chinese (if suited) are allowed for answering the questions. We do not accept any other languages. ┌─────────────────────────────────────┐ │There are 10 questions in the exam, each worth 20 points ─ the full │ │credit is 200 points. For the 10 questions, 3 of them are marked with * │ │and are supposedly simple; 4 of them are marked with ** and are supposedly│ │regular; 3 of them are marked with *** and are supposedly difficult. The │ │questions are roughly ordered by the difficulty level. │ └─────────────────────────────────────┘ (1) (20%, *) George and Mary decide to be together forever (a.k.a. 一直走下去). To symbolize their love to each other, they decide to exchange their very first gifts. For the following code, the C++ function exchange is supposed to do the task, but the result appears incorrect. (a) Illustrate (with drawing) what the memory layout is for George, Mary, boy, girl at //DRAW. (b) Modify the code to complete the exchange successfully, without using any pointers. Please simply highlight which line you want to change (you only need to change one!), and write down how you want to change it. 1 typedef GIFT int; 2 struct Person { GIFT gift; }; 3 4 void exchange(Person boy, Person girl){ 5 GIFT onTable = girl.gift; 6 girl.gift = boy.gift; 7 boy.gift = onTable ; 8 //DRAW 9 } 10 11 Person George, Mary; 12 13 int main(){ 14 //some more code to let George and Mary prepare gifts 15 exchange(George, Mary); 16 //now George should take Mary's gift, and Mary takes George's 17 return 0; 18 } (2) (20%, *) Considering representing a queue by a fixed-size circular array of size 4, where the front and the tail of the (empty) queue are both at 0 in the beginning. After executing each step of the following operations sequentially, list the contents of the array, as well as where head and tail are. For locations without contents, please use a special character to represent them. enqueue(1); enqueue(3); dequeue(); enqueue(2); enqueue(4); dequeue(); enqueue(5); enqueue(6); dequeue(); dequeue(); (3) (20%, **) Consider having two stacks for storing integers, a red one and a blue one with the push and pop operations only. Illustrate how to implement the int pop_mth(color s, int m) operation that pops the m-th element from the top (and only that element) from stack s. Note that pop_mth(s, 1) should be equivalent to the usual pop from stack s, and after the operation, all other elements should remain in their original orders. (4) (20%, **) The following code causes illegal memory access. (a) Illustrate (with drawing) what the memory layout is at //DRAW. (b) Explain why the //SEGFAULT line could lead to illegal memory access. 1 class VectorBad{ 2 public: 3 int* data ; 4 VectorBad(){ data = new int[2]; } 5 ~VectorBad(){ delete[] data; } 6 } 7 }; 8 9 int main(){ 10 VectorBad a; a.data[0] = 1; a.data[1] = 3; 11 { 12 VectorBad temp = a; 13 for(int i = 0; i < 2; i++) temp.data[i] += (i * i); 14 //DRAW 15 } 16 a.data[1] = 5; //SEGFAULT 17 return 0; 18 } (5) (20%, **) The following recursive code reverses a singly-linked list. 1 void reverseList(Node* head){ 2 if(head->next){ 3 Node* tail = head->next; 4 reverseList(head->next) 5 tail->next = head; 6 } 7 } (a) Illustrate how the code reverses a linked list A->B->C when calling reverseList(A). You need to describe the steps clearly for the TAs. (b) Complete the following tail-recursive code that also reverses the linked list. 1 void reverseListAndAppend(Node* head, Node* append){ 2 if(head->next){ 3 Node* tmp = head->next; 4 head->next = append; 5 //what to put here? reverseListAndAppend(..., ...); 6 } 7 else{ 8 //what to put here? 9 } 10 } 11 void reverseList(Node* head){ 12 //what to put here? reverseListAndAppend(..., ...) 13 } (6) (20%, *) The lower-triangular matrix is a matrix M with M[r][c] = 0 whenever c > r. Naturally, we do not want to waste storage on the 0 part. A common way of storing a lower-triangular matrix is called a "rectangular representation." For instance, the content of a 4 by 4 lower triangular 10 9 1 0 0 0 1 6 matrix 2 3 0 0 can fit in a 5 by 2 rectangular matrix 2 3 . 4 5 6 0 4 5 7 8 9 10 7 8 Following the example above, design a data structure that implements the rectangular storage scheme for an arbitrary lower-triangular matrix (Hint: discuss cases of even or odd number of columns). Illustrate the scheme clearly and discuss how to access the element at row r and column c of the lower-triangular matrix. ┌─────────────────────────────────────┐ │For the following two questions, you can only use this definition: │ │Let f, g be functions from nonnegative integers to real numbers. We say │ │f(n) = O(g(n)) if there is a real constant c > 0 and an integer constant │ │n_0 ≧ 1 such that f(n) ≦ cg(n) for n ≧ n_0. │ └─────────────────────────────────────┘ (7) (20%, **) Prove or disapprove the following statement. "For non-negative functions f, g, h, if f(n) = O(g(n)), then f(n) + h(n) = O(g(n) + h(n))." (8) (20%, ***) Prove or disapprove the following statements. (a) "If a < 1 or (a = 1 and b ≦ 0), then 2^(an^2 + bn + c) = O(2^(n^2))." (b) "If 2^(an^2 + bn + c) = O(2^(n^2)), then a < 1 or (a = 1 and b ≦ 0)." (9) (20%, ***) A linked list allows a node to link to the next one, which can be slow if you want to access a node far far away. Consider the following multiply-linked list, which allows a node store K pointers, each linking to next-1, next-2 (a node two steps away), next-4 (a node four steps away), next-8, …, next-2^(K-1). Finish the code for getDataNext. Which walks s steps from the current node and then return the pointer to the destination node, and NULL otherwise. Your code should run within O(logN) of time complexity, where N is the number of elements in the multiply-linked list. Briefly explain why your code runs within O(logN). 1 #define K (32) 2 class MLNode{ 3 public: 4 int data; 5 MLNode next[K]; //next[i] links to a node (2^i) steps away 6 //or NULL if the node does not exist 7 8 MLNode* getDataNext(unsigned int s){ 9 if (s == 0) return this; 10 //FINISH THE REST OF THE CODE 11 } 12 }; (10) (20%, ***) Consider an array a of N integers a_0 < a_1 < a_2 < … < a_M and a_M > a_(M+1) > … > a_(N-1). In other words, a_M is the maximum element of the array. Write down an O(logN)-time algorithm that finds the value of a_M. Briefly describe why the algorithm is correct. (Hint: think about binary search.) --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.228.165.134
※ 文章網址: https://webptt.com/m.aspx?n=bbs/NTU-Exam/M.1481346842.A.32A.html
1F:→ rod24574575 : 已收資訊系! 12/10 15:37







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:e-shopping站內搜尋

TOP