Explain each of the following terms. (12%) (a) O(n 2 ) (b) protected in C++ language (c) sparse matrix 7. Write

Similar documents
Microsoft Word - data_mid1611_and_sol.docx

ENGG1410-F Tutorial 6

Microsoft PowerPoint - Lecture7II.ppt

C/C++程序设计 - 字符串与格式化输入/输出

CC213

Microsoft PowerPoint - STU_EC_Ch02.ppt

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

C/C++ - 文件IO

C 1

L L

C/C++语言 - C/C++数据

2015 Chinese FL Written examination

Microsoft Word - 11月電子報1130.doc

<4D F736F F F696E74202D20B5DAD2BBD5C228B4F2D3A1B0E6292E BBCE6C8DDC4A3CABD5D>

Microsoft Word - Final Exam Review Packet.docx

Microsoft Word

C C

Open topic Bellman-Ford算法与负环

TX-NR3030_BAS_Cs_ indd

LEETCODE leetcode.com 一 个 在 线 编 程 网 站, 收 集 了 IT 公 司 的 面 试 题, 包 括 算 法, 数 据 库 和 shell 算 法 题 支 持 多 种 语 言, 包 括 C, C++, Java, Python 等 2015 年 3 月 份 加 入 了 R

C/C++ - 数组与指针

C++ 程式設計

Microsoft PowerPoint - STU_EC_Ch04.ppt

2/80 2

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

Microsoft Word doc

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

Microsoft PowerPoint - Model Checking a Lazy Concurrent List-Based Set Algorithm.ppt [Compatibility Mode]

Stochastic Processes (XI) Hanjun Zhang School of Mathematics and Computational Science, Xiangtan University 508 YiFu Lou talk 06/

第1章 簡介

ebook39-5

Microsoft PowerPoint - STU_EC_Ch08.ppt

untitled

2017 CCAFL Chinese in Context

3.1 num = 3 ch = 'C' 2

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

4. 每 组 学 生 将 写 有 习 语 和 含 义 的 两 组 卡 片 分 别 洗 牌, 将 顺 序 打 乱, 然 后 将 两 组 卡 片 反 面 朝 上 置 于 课 桌 上 5. 学 生 依 次 从 两 组 卡 片 中 各 抽 取 一 张, 展 示 给 小 组 成 员, 并 大 声 朗 读 卡

States and capital package

untitled

Microsoft Word - template.doc

CC213

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc


untitled

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

Chapter12 Derived Classes

C/C++ 语言 - 循环

Microsoft Word 記錄附件

台 灣 人 權 學 刊 第 三 卷 第 三 期 他 還 接 受 教 育 部 的 委 託, 長 年 擔 任 中 央 層 級 的 人 權 教 育 輔 善 團 的 指 導 教 授, 至 今 已 有 多 年 我 雖 然 不 是 很 了 解 為 什 麼 他 可 以 一 邊 承 擔 教 育 部 賦 予 的 任

Microsoft Word - Book9

Windows XP

第3章.doc

92 (When) (Where) (What) (Productivity) (Efficiency) () (2) (3) (4) (5) (6) (7) em-plant( SiMPLE++) Scheduling When Where Productivity Efficiency [5]

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40


国 培 简 讯 国 培 计 划 (2012) 示 范 性 集 中 培 训 项 目 国 培 计 划 (2012) 中 小 学 教 师 示 范 性 集 中 培 训 暨 中 西 部 农 村 教 师 集 中 培 训 中 小 学 骨 干 教 师 北 京 外 国 语 大 学 英 语 学 科 研 修 项 目 毕


˛ˇ

1990 8

四川省普通高等学校

Microsoft Word 管理學

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

336 共分五節 首先爬梳傳統莊周試妻戲曲的淵源本事 從中溯源配角人物的原型 其次三節 依劇情節推展所出現之配角人物依序論述 夢境骷髏 乃試妻背景之 啟示者 搧墳寡婦 則是試妻動機直接的引發者 僮僕與紙人 則是試妻過程 中的參與者 每節再從原型論述到傳統諸作 第五節則綜合探討傳統莊周試妻戲 曲中配角

Microsoft Word - A doc

Lorem ipsum dolor sit amet, consectetuer adipiscing elit

Logitech Wireless Combo MK45 English


int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

(Guangzhou) AIT Co, Ltd V 110V [ ]! 2

(baking powder) 1 ( ) ( ) 1 10g g (two level design, D-optimal) 32 1/2 fraction Two Level Fractional Factorial Design D-Optimal D

01何寄澎.doc

Microsoft Word - 100碩士口試流程

2 Edmonton 爱 德 蒙 顿 爱 德 蒙 顿 是 加 拿 大 的 节 日 之 城, 一 年 有 超 过 30 多 个 节 日 城 市 总 人 口 1000 多 万 干 净, 安 全 的 居 住 环 境 友 好 的, 充 满 活 力 的 文 化 社 区 附 近 有 许 多 风 景 优 美 的

中国科学技术大学学位论文模板示例文档

Untitled-3

<4D F736F F F696E74202D20A46ABEC7A6DBBFECB5FBC5B2AABAC0B3A6B3A740ACB0BB50B9EAB0C8B1B4AA522E >

三維空間之機械手臂虛擬實境模擬

d y d = d 2 y d 2 = > 0 Figure45 :Price consumer curve not a Giffen good X & Y are substitutes From the demand curves. Figure46 Deman

WTO

K301Q-D VRT中英文说明书141009

ch_code_infoaccess

ebook39-6

威 福 髮 藝 店 桃 園 市 蘆 竹 區 中 山 里 福 祿 一 街 48 號 地 下 一 樓 50,000 獨 資 李 依 純 105/04/06 府 經 登 字 第 號 宏 品 餐 飲 桃 園 市 桃 園 區 信 光 里 民

第六章 中国中等收入者调查的三个发现

GCSE Mathematics Question Paper Unit 2 March 2012

高中英文科教師甄試心得

Microsoft PowerPoint - 7.ppt

ebook14-4

Microsoft Word - 新加坡手冊封面.docx

you have a dream, you got to protect it

Microsoft Word doc

Microsoft PowerPoint - Eisenstein_ABET_Presentation_Beijing_Oct_2007-Chinese.ppt [兼容模式]


Microsoft Word - CX VMCO 3 easy step v1.doc

Microsoft PowerPoint _代工實例-1

Book1

untitled

第5章修改稿

2

Transcription:

Department of Computer Science and Engineering National Sun Yat-sen University Data Structures - Middle Exam, Nov. 20, 2017 1. Suppose an array is declared as a[5][6][4], where the address of a[0][0][0] is 200 and each element requires four bytes. Please give the addresses of a[3][4][2] with the row-major representation and the column-major representation. (8%). 2. What are printed by each of the following C programs? (16%) (a) char e=13; printf("%d \n",~((e+4) >> 3)); (b) void f(int a[ ], int b[ ], int *c, int *d) printf("%d %d %d %d \n", a[1],b[2],*(c+2),d[4]); } int main( ) int e[ ]=20,21,22,23,24,25,26,27,28,29,30}; f(e,e+3,&e[2],&e[1]+4); } (c) int a[ ]=11,14,17,20,23,26}; int *p; p=a; *(p++)=5; (*(++p))++; printf("%d %d %d %d \n",a[0],a[1],*p,*(p+2)); (d) union char m; unsigned char n; }u; u.n=193; printf("%d \n",u.m); 3. Please draw the expression tree of the infix expression (A+B)*D+E/(F+A*D)+C, and then give the prefix and postfix forms. (9%) 4. John is learning numeric symbols (1,2,3,4 ), but sometimes he may write 1 as L. With only the first three digits (1, 2, 3, L) for addition, we want to know the number of permutations whose sum is n. For example, if n=2, the 5 permutations have the same sum 2: 11, 1L, L1, LL, 2. If n=3, the 13 permutations have the same sum 3: 111,11L, 1L1,1LL, L11, L1L, LL1, LLL, 21,2L, 12, L2,3. Let f(n) denote the number of permutations with sum n. Then f(n) can be calculated by the recurrence formula: f(n) = a f(n-1)+b f(n-2)+c f(n-3), n 4. What are the values of a, b and c? (9%) 5. Suppose that a matrix m[ ][ ] is stored in a linear array a[ ] with the sequence in the following figure. Please give the mapping function from m[i][j] to a[k], that is, to express k as a function of i and j. Note that the upper left corner of m is the first element m[0][0], the first element of a is a[0]. And m[0][1] = 1, m[0][2] = 5, and (10%) - 1 -

0 1 5 6 14 2 4 7 13 3 8 12 9 11 10 6. Explain each of the following terms. (12%) (a) O(n 2 ) (b) protected in C++ language (c) sparse matrix 7. Write a recursive C/C++ function to perform the binary search on a nondecreasingly sorted array. (12%) int BSearch(int a[ ], int x, int left, int right) // a[ ]: nondecreasingly sorted array // search for x in a[left], a[left+1],, a[right-1], a[right] //Return the index if found. Return -1 if not found. Please write the body of BSearch( ). } // end of BSearch( ) 8. Write a C/C++ function to perform insert (into the rear) and remove (from the front) operations of a circular queue implemented with an array. (12%) int front, rear; // front, rear pointers int capacity=100; // size of queue char q[100]; // array for the circular queue. //No data element is stored in a[front], but a[rear] stores one element. void Insert(char x) // insert x into the rear. You have to check if q is full before the insertion. (a) Please write the body of Insert( ). } // end of insert ( ) char Remove(void) // Remove an element from the front, and return the removed element. // You have to check if q is empty before the removal. (b) Please write the body of Remove( ). - 2 -

} // end of Remove( ) 9. Let x=(x 1, x 2,, x m-1, x m ) and y=(y 1, y 2,, y n-1, y n ) be two circular chains. Write a C++ function to concatenate the two circular chains into a circular chain z=(x 1, x 2,, x m-1, x m, y 1, y 2,, y n-1, y n ). Note that x or y may be empty. (12%) class ChainNode int data; ChainNode *link; // Point to the next node }; class Chain ChainNode *first *last; // circular chain Chain concatenate(chain &y ) // y is concatenated to the end of *this (x) // You have to consider empty chains. Chain z; // The resulting chain Please write the body of concatenate( ). return z; } // end of concatenate( ) }; - 3 -

Answer: 1. row-major: 200 + 4 * (3 * 6 * 4 + 4 * 4 + 2) = 200 + 4 * 90 = 560 column-major: 200+ 4* (2 * 6 * 5 + 4 * 5 + 3) = 200 + 4 * 83 = 532 2. (a) ~((13 + 4) >> 3) = ~(17 >> 3) = ~ ((00010001) 2 >> 3) = ~((00000010) 2 ) = (11111101) 2 = -3 由於 e 是 char 資料型態, 故以 8 bits 呈現 印出 e 時, 是以 %d 表現, 亦即是帶有正負號之整數, 因此需將當時的數值解讀為 2 s complement Output: -3 (b) a[0] 對應至 e[0], 因此 a[1] = e[1] = 21 b[0] 對應至 e[3], 因此 b[2] = e[5] = 25 c 對應至 e[2]( 也就是 c[0] 對應至 e[2]), 因此 *(c+2) = e[2+2] = 24 d 對應至 e[5] ( 也就是 d[0] 對應至 e[5]), 因此 d[4] = e[9] =29 Output: 21 25 24 29 (c) p=a; // p 對應至 a[0] *(p++) = 5; // 先執行 *p =5, 因此 a[0]=5 再做 p=p+1, 因此 p 對應至 a[1] (*(++p))++; // 先做 p=p+1, 因此 p 對應至 a[2] 再做 (*p)++, 即 a[2]=17+1=18 a[0] = 5 a[1] = 14 *p =a[2]=18 *(p+2) a[4]= 23 Output: 5 14 18 23 (d) u 與 v 佔據相同記憶體位置, 兩者的資料內容相同, 但解讀方式不同 u 為無正負號的 char, 亦即為 8 bit 無正負號之整數 m 亦是 8 bit, 但帶有正負號 將 193 轉換成 2 s complement 之負值即可 u=193 (10) = 11000001 (2), 2 s complement 為 63 (10) = 00111111 (2), 故 11000001 (2) 為 -63 Output: -63 Summary: (a) -3 (b) 21 25 24 29 (c) 5 14 18 23 (d) -63 3. Prefix: ++*+ABD/E+F*ADC Postfix: AB+D*EFAD*+/+C - 4 -

4. f(n) 可以表示如下 : f(n) = 2f(n-1) + f(n-2) + f(n-3) 2f(n-1) 在 f(n-1) 每項之後再加上 1 及 L, 可使總和由 n-1 增加為 n f(n-2) 在 f(n-2) 每項之後再加上 2, 可使總和由 n-2 增加為 n f(n-3) 在 f(n-3) 每項之後再加上 3, 可使總和由 n-3 增加為 n Summary: a=2, b=1, c=1 5. 計算 [i][j] 的編號時, 在此之前的斜線 ( 圖中藍色三角形 ), 共有如下的數字個數 : 1+2+3+ +(i+j)= (i+j)(i+j+1) / 2 之後再依 i + j 為奇數或偶數偶, 判斷 [i][j] 所在斜線, 尚需增加之個數 ( 例如圖中 [1][3] 位置, 尚需增加 j=3, 因為 i+j 為偶數 ) 完整公式如下 : k=[1+2+ +(i+j)]+i= (i+j)(i+j+1) / 2 +i,if i+j is odd ( 奇數 ) k=[1+2+ +(i+j)]+j= (i+j)(i+j+1) / 2 +j,if i+j is even ( 偶數 ) 6. (a) O(n 2 ): 至多與 n 2 成正比, 可用來表示時間複雜度或空間複雜度 (b) 能被原本的 class 以及衍生的 class( 繼承者 ) 存取 - 5 -

(c) 一個矩陣中, 大部分元素為零, 少數為非零 7. if(left > right) return -1; int mid = (left + right)/2; if(a[mid] == x) return mid; if(a[mid] > x) Bsearch(a, x, left, mid-1); if(a[mid] < x) Bsearch(a, x, mid +1, right); 8. (a) (b) if((rear + 1)%capacity) == front) throw full ; rear = (rear + 1)%capacity; q[rear] = x; if(rear == front) throw empty ; front = (front + 1)%capacity; return q[front]; 9. if( front == NULL ) // x is NULL z.first = y.first; z.last = y.last; } else if( y.first == NULL ) // y is NULL z.first = first; z.last = last; } else // Both x and y are not NULL last link = y.first; // last of x points to first of y y.last link = first; // last of y points to first of x, for circular chains z.first = first; z.last = y.last } return z; - 6 -