b04902xxx 板


LINE

※ [本文转录自 NTU-Exam 看板 #1Lko34Bp ] 作者: rod24574575 (天然呆) 看板: NTU-Exam 标题: [试题] 103下 林轩田 资料结构与演算法 期中考+解答 时间: Fri Jul 31 15:16:17 2015 课程名称︰资料结构与演算法 课程性质︰必修 课程教师:林轩田 开课学院:电资学院 开课系所︰资工系 考试日期(年月日)︰2015.04.21 考试时限(分钟):120分钟 试题 : Midterm Examination Problem Sheet TIME: 04/21/2015, 14:20-16:20 This is a 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 5 problems in the exam, each worth 40 points─the full credit is│ │200 points. Some problems come with two sub-problems to help you get │ │partial credits, making a total of 9 sub-problems. For the 9 problems, 3 │ │of them are marked with * and are supposedly simple; 4 of them are marked │ │with ** and are supposedly regular; 2 of them are marked with *** and are │ │supposedly difficult. │ └─────────────────────────────────────┘ 1. C and C++ (a) (20%, *) Give a concrete example in C/C++ to explain in your own words on how memory leak could happen. Ans: int &func(){ int *res = new int(1126); return (*res); } int a = func(); In the code above, "res" is allocated in func, and then (*res) is copied to a. So the space pointed by res can never be freed, and becomes "garbage" (memory leak) in the memory. (b) (20%, *) Suppose we use the following class to represent the homework scores of a student in DSA class: 1 class myScore { 2 public: 3 myScore(int n = 10){ size = n; hwScore = new int[size]; } 4 ~myScore(){ delete[] hwScore; } 5 string name; 6 int *hwScore; 7 }; Draw the memory layout after running the following code snippet, and explain the potential hazards/problems of the code. 1 myScore a(6); 2 a.name = "John"; a.hwScore[0] = 85; a.hwScore[1] = 90; 3 myScore b = a; 4 b.name = "Mary"; b.hwScore[0] = 70; Ans: a b ┌────────┐ ┌────────┐ name│ "John" │ name│"John" → "Mary"│ ├────────┤ ├────────┤ hwScore│ │ hwScore│ │ └────┼───┘ └────┼───┘ │ ┌────────┘ │ ↓ │ ┌───┬──┬──┬──┬──┬──┐ └──→│85→70│ 90 │ 0 │ 0 │ 0 │ 0 │ └───┴──┴──┴──┴──┴──┘ The copy constructor called at "myScore b = a" copies the address of a.hwScore to b.hwScore, making the two objects share the same array which is wrong and dangerous (In destructor, there can be double-free) 2. arrays and linked lists (a) (20%, **) Assume that you have a vector of N integers that are between 1 and M. Write down the pseudo code of an O(N)-time O(M)-space algorithm that determines whether all the integers are different from each other (that is, whether they are all distinct). Ans: ‧ initialize an array count[1…M] with 0 ‧ for i ← 1 to N count[i-th element of vector] increased by 1 if count[i-th element of vector] > 1 return FALSE ‧ return TRUE (b) (20%, *) Suppose we have a singly linked list with each node defined by the following class: 1 class node { 2 public: 3 string data; 4 node *next; 5 } Write a C/C++ function invert(node *head) that can invert the list pointed to by head and return the pointer to the inverted list without doing any dynamic memory allocation. (Hint: Note that your function should not need more than 20 lines.) Ans: node *invert(node *head) { node *current = head; node *prev = NULL; while(current->next != NULL){ node *tmp = curent->next; current->next = prev; prev = current; current = tmp; } current->next = prev; return current; } 3. stacks and queues (a) (20%, **) Show how a stack can be used to evaluate the postfix expression 9 8 7 2 3 * - / - 5 2 - * 3 / step by step. All the operators within the expression are binary, and you should draw the stack status after each step. Ans: word stack 9 9 8 98 7 987 2 9872 3 98723 * 9876 - 981 / 98 - 1 5 15 2 152 - 13 * 3 3 33 / 1 (b) (20%, **) Suppose you have a deque D that stores N elements (d_0, d_1, ..., d_(N-1)), in this order, and an initially empty queue Q. Give a pseudo-code description of a function that uses only D and Q (and no other variables or objects) such that the elements within D are reversed─that is, are stored like (d_(N-1), d_(N-2), ..., d_0) in D. Ans: ‧ while not D.empty() Q.enqueue(D.pop_back()) ‧ while not Q.empty() D.push_back(Q.dequeue()) 4. the evil complexity ┌───────────────────────────────────┐ │The notation f(n) = O(g(n)) applies to functions f(n) and g(n) from N │ │to R+∪{0}, and f(n) = O(g(n)) if and only if there exists n_0 ≧ 1 │ │and c > 0 such that ∀n ≧ n_0, f(n) ≦ cg(n). Please only use the │ │definition above in your proof. │ └───────────────────────────────────┘ (a) (20%, **) Prove or disprove the following statement: If f(n) = O(g(n)), then (f(n))^2 = O((g(n))^2). Ans: if f(n) = O(g(n)) ∃ n_0 ≧ 1 and c > 0 s.t. f(n) ≦ c·g(n) for n ≧ n_0 then by f(n) ≧ 0 (f(n): N → R+∪{0}) for n ≧ n_0, (f(n))^2 ≦ c·g(n)·f(n) ≦ c·g(n) · c·g(n) = (c^2)·(g(n))^2 let n_1 = n_0, c_1 = c^2 from above, for n ≧ n_1, (f(n))^2 ≦ c_1·(g(n))^2 that is, (f(n))^2 = O((g(n))^2) (b) (20%, ***) Prove or disprove the following statement: If |f(n) - g(n)| = O(1), then 2^(f(n)) = O(2^(g(n))). Ans: if |f(n) - g(n)| = O(1) ∃ n_0 ≧ 1 and c > 0 s.t. |f(n) - g(n)| ≦ c for n ≧ n_0 then f(n) - g(n) ≦ |f(n) - g(n)| ≦ c that is f(n) ≦ g(n) + c => 2^(f(n)) ≦ 2^(g(n))·2^c let n_1 = n_0, c_1 = c^2 from above, for n ≧ n_1, 2^(f(n)) ≦ c_1·2^(g(n)) that is, 2^(f(n)) = O(2^(g(n))) 5. (40%, ***) Given a sorted (ordered) vector a with N distinct integers where a[0] < a[1] < ... < a[N-1]. The N integers define N+1 non-overlapping ranges (-∞, a[0]], (a[0], a[1]], ..., (a[N-1], ∞) in R. Given a value x, a common task is to determine the range that x falls in. Write down the pseudo code of a (logN)-time algorithm that determines the range. For simplicity, you can assume that x is different from all elements in a. (Hint: Think about binary search and finding the largest a[n] that is smaller than x) Ans: func. find Range (a, N, X) if x ≦ a[0] return (-∞, a[0]] else if x > a[N-1] return (a[N-1], ∞) else left ← 0 right ← N-1 while(left < right - 1){ mid ← floor((left + right) / 2) if a[mid] ≧ x right ← mid else left ← mid } return (a[left], a[right]) --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.162.254.199
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/NTU-Exam/M.1438326980.A.2F3.html
1F:→ rod24574575 : 已收资讯系! 07/31 15:17
2F:推 kevin1ptt : c^2 应该改成 2^c 02/13 00:57



※ 发信站: 批踢踢实业坊(ptt.cc)
※ 转录者: w4a2y4 (140.112.4.192), 03/14/2016 19:31:57







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灯, 水草

请输入看板名称,例如:BuyTogether站内搜寻

TOP