Microsoft Word - CPMidTerm2011SpringSolution

Similar documents
Microsoft Word - CPMidTerm2011Spring

Microsoft Word - CPMidTerm2010SpringSolution

untitled

Object-Oriented Programming, Mid-term Test, 11/21/2000

Microsoft Word - 第3章.doc

untitled

Microsoft Word - CPMidTerm2012SpringSolution

untitled

untitled

untitled

CHAPTER VC#

untitled

新・解きながら学ぶJava

C/C++ - 字符输入输出和字符确认

新版 明解C++入門編

<4D F736F F D DA5BFA6A1C476C1C92DBEC7ACECB8D5A8F728B57BB35D292E646F63>

《大话设计模式》第一章

CC213

<4D F736F F D B0D3B77EC3FEA7DEC3C0C476C1C9A5BFA6A1B8D5C3442DB57BA6A1B35DAD702DBEC7ACEC2E646F6378>

目 錄 內 容 頁 數 1. 我 們 的 學 校 3 2. 關 注 事 項 的 成 就 與 反 思 我 們 的 學 與 教 我 們 對 學 生 的 成 長 支 援 學 生 表 現 財 務 報 告 ( 年 度 ) 22-23

全国计算机技术与软件专业技术资格(水平)考试

CHAPTER 1

第3章.doc

Microsoft Word - 01.DOC

個 人 的 手, 拉 著 瞎 子 的 手 把 他 帶 往 村 外 的 時 候, 對 於 瞎 子 來 講, 那 個 人 的 手 和 耶 穌 的 手 有 沒 有 區 別? 沒 有! 為 什 麼 沒 有 區 別? 因 為 對 於 一 個 瞎 子 來 說, 手 和 耳 朵 就 是 他 接 觸 世 界, 瞭

FY.DOC

新・解きながら学ぶC言語

Microsoft Word - C-pgm-ws2010.doc

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

碩命題橫式

新版 明解C言語入門編

新・明解C言語入門編『索引』

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

概述

第1章

Microsoft Word - ch04三校.doc

untitled

第 二 單 元 我 過 去 都 做 了 什 麼 決 定? 將 生 涯 建 構 論 建 構 理 論 應 用 在 課 程 當 中, 就 是 敘 事 取 向 的 生 涯 諮 商, 他 們 利 用 幻 遊 個 人 傳 記 及 說 故 事 等 方 式, 讓 案 主 從 中 去 挖 掘 或 發 現 自 己 過

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

Microsoft Word - CPFinal2010Spring

untitled

C H A P T E R 7 Windows Vista Windows Vista Windows Vista FAT16 FAT32 NTFS NTFS New Technology File System NTFS

untitled

第一章 章标题-F2 上空24,下空24

BOOL EnumWindows(WNDENUMPROC lparam); lpenumfunc, LPARAM (Native Interface) PowerBuilder PowerBuilder PBNI 2

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

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

untitled

Microsoft PowerPoint - Chap03.ppt [相容模式]

主程式 : public class Main3Activity extends AppCompatActivity { ListView listview; // 先整理資料來源,listitem.xml 需要傳入三種資料 : 圖片 狗狗名字 狗狗生日 // 狗狗圖片 int[] pic =new

C++ 程序设计 告别 OJ1 - 参考答案 MASTER 2019 年 5 月 3 日 1

2015级“园林技术”专业人才培养方案

第5章修改稿

JavaIO.PDF

3.1 num = 3 ch = 'C' 2

untitled

Microsoft PowerPoint - Class2.pptx

Microsoft PowerPoint - ch1.pptx

CC213

Java 程式設計初階 第 5 章:基本輸出入 & 流程控制

000

エスポラージュ株式会社 住所 : 東京都江東区大島 東急ドエルアルス大島 HP: ******************* * 关于 Java 测试试题 ******

内 容 提 要 指 针 持 久 动 态 内 存 分 配 字 符 串 ( 字 符 数 组 ) 2

【主持人】:给大家介绍一下,这次的培训是我们画刊部的第三次培训,当然今天特别有幸请来著吊的摄影家李少白老师给我们讲课


Microsoft Word - 1-1泰宇解答

Microsoft PowerPoint - ch04_AEL0080.ppt


<4D F736F F D20D1A7C9FACAD6B2E1B8C4D7EED6D5A3A8B4F8B1EDB8F1BCD3D2B3C2EBB0E6A3A9372E3239>

桂林市劳动和社会保障局关于

第三章 維修及管理

Microsoft Word 年度选拔硕博连读研究生的通知.doc

C/C++ - 文件IO

EJB-Programming-4-cn.doc

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

科学计算的语言-FORTRAN95

_汪_文前新ok[3.1].doc

untitled

Microsoft Word - CPFinal2011SpringSolution

Microsoft PowerPoint - 20-string-s.pptx

untitled

C/C++语言 - 分支结构

untitled

Spyder Anaconda Spyder Python Spyder Python Spyder Spyder Spyder 開始 \ 所有程式 \ Anaconda3 (64-bit) \ Spyder Spyder IPython Python IPython Sp

C/C++ - 函数

Excel VBA Excel Visual Basic for Application

Microsoft PowerPoint - Lecture7II.ppt

書本介紹


t11phip

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

Microsoft PowerPoint - ch2.pptx

Microsoft PowerPoint - 12 struct and other datatypes.ppt

(6) 要 求 付 款 管 理 员 从 预 订 表 中 查 询 距 预 订 的 会 议 时 间 两 周 内 的 预 定, 根 据 客 户 记 录 给 满 足 条 件 的 客 户 发 送 支 付 余 款 要 求 (7) 支 付 余 款 管 理 员 收 到 客 户 余 款 支 付 的 通 知 后, 检

Microsoft Word - ACL chapter02-5ed.docx

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

填 写 要 求 1. 以 word 文 档 格 式 如 实 填 写 各 项 2. 表 格 文 本 中 外 文 名 词 第 一 次 出 现 时, 要 写 清 全 称 和 缩 写, 再 次 出 现 时 可 以 使 用 缩 写 3. 本 表 栏 目 未 涵 盖 的 内 容, 需 要 说 明 的, 请 在

CC213

Microsoft Word - 04_object_Boxing_property_indexer.doc

Transcription:

通識計算機程式設計期中考參考解答, 4/22/2011 1. (a) 宣告 int 變數 k, bool 變數 b, double 變數 x (3%) 答 : int k; bool b; double x; (b) 在螢幕顯示一行字, 要求使用者輸入一個整數 (3%) 答 : Console.WriteLine(" 輸入一個整數 "); (c) 自鍵盤讀入一個整數., 並將其值存入已宣告之 int 變數 k (3%) 答 : k = int.parse(console.readline()); 或 k = Convert.ToInt16(Console.ReadLine()); (d) 令已宣告之 bool 變數 b 等於邏輯敘述 : k 被 2 除之餘數等於 0 (3%) 答 : b = (k % 2 == 0); (e) 若.b 為真, 在螢幕顯示 偶數 否則印出 奇數 (3%) 答 :if (b) Console.WriteLine(" 偶數 "); else Console.WriteLine(" 奇數 "); 或 string message = b? " 偶數 " : " 奇數 "; Console.WriteLine(message); 2. (a) 只用一個敘述, 將已宣告設值之 int 變數 n 以遞減算子減 1 後, 再設值 (assign) 給他處已宣告之 int 變數 m (3%) 答 : m = --n; (b) 宣告 bool 變數 r, 並設其值為 ( 非 p) 或 (or) q 之邏輯演算的結果 1/14

bool 變數 P 與 q 均已於他處已宣告設值 (3%) 答 : r =!p q; (c) 宣告 double 變數 y, 並令其值為他處已宣告設值之 double 變數 t 與 2 之積的餘弦函數 (cosine) 值, 注意此處 為圓周率 (3%) 答 : double y = Math.Cos(2.0 * Math.PI * t); (d) 宣告 string 變數 state, 利用三元運算子使其在他處已宣告設值之 double 變數 membranepotential 大於等於 -55 時等於 激發, 反之則等於 靜態 (3%) 答 : string state = (membranepotential >= -55)? " 激發 " : " 靜態 "; (e) 宣告變數 c 為 char 型別, 並令其值為倒斜線 (back slash) 字元 (3%) 答 : char c = '\\'; 3. (a) 宣告一個 int 常數 N, 其值為 7 (3%) 答 : const int N = 7; (b) 宣告一個列數與行數均為為 N 的 int 二維陣列, 命名為 a (3%) 答 : int[,] a = new int[n, N]; (c) 宣告一個亂數產生器物件 rand, 種子數為 777 (3%) 答 :Random rand = new Random(777); (d) 將每個 a 陣列元素設為一個由 rand 產生, 在 2( 含 ) 與 12( 含 ) 之間的亂數 (3%) 答 : int i; int j; for(i=0; i<n; ++i) for (j = 0; j < N; ++j) a[i, j] = rand.next() % 11 + 2; 2/14

(e) 計算 a 陣列各列的總和, 存入一維 int 陣列 rowsum 的對應元素 令 rowsum 已於他處宣告 (3%) 答 : for (i = 0; i < N; ++i) rowsum[i] = 0; for (j = 0; j < N; ++j) rowsum[i] += a[i, j]; 4.. (a) (3%) int 1stPlace = 1; 答 : 變數名稱不可以數字開始 更正 : int FirstPlace = 1; (b) (3%) 以下程式片段應產生三種輸出之一 : char age = Console.ReadLine().ToCharArray()[0]; switch (age) case ' 少 ': Console.WriteLine(" 紅燭昏羅帳 "); case ' 壯 ': Console.WriteLine(" 斷雁叫西風 "); default: Console.WriteLine(" 鬢已星星也 "); 答 : case 及 default 結束時須加上 break 敘述更正 : char age = Console.ReadLine().ToCharArray()[0]; switch (age) case ' 少 ': Console.WriteLine(" 紅燭昏羅帳 "); break; case ' 壯 ': Console.WriteLine(" 斷雁叫西風 "); break; default: Console.WriteLine(" 鬢已星星也 "); break; 3/14

(c) (3%) int i; while( i <= 3 ) Console.WriteLine( i/(i-1) ); ++i; 答 : i = 1, i/(i-1) 會發生除以 0 的例外 更正 ( 合理的其他更正方式亦可 ): int i = 1; while (i <= 3) ++i; Console.WriteLine(i / (i - 1)); (d) (3%) 以下程式片段執行後, 應出現如後畫面 : for( int i=0; i<3; ++i ) Console.WriteLine( 0 計程再累, 我也要堅持到底!, i++); 答 : 迴圈中控制變數 i 發生改變, 使用到的 i 值只有 0, 2 兩個, 使輸出為 更正 : for (int i = 0; i < 3; ++i) Console.WriteLine( "0 計程再累, 我也要堅持到底!", i+1); 4/14

(e) (3%) 下列程式片段應定義兩個函式. static void f1(int x) Console.WriteLine( f1(0), x); static void f2(int x) Console.WriteLine( f2(0), x); 答 : 函式 f2 不應定義在函式 f1 的一對大括弧之間. 更正 : static void f1(int x) Console.WriteLine("f1(0)", x); static void f2(int x) Console.WriteLine("f2(0)", x); 5. 試寫出下列程式的輸出 (5%) using System; namespace MidTerm2011Problem5 class Program static void Main(string[] args) int[] a = 4, 5, 6 ; int[] b = a; WhatDoesThisDo(a); Console.WriteLine("a[2] = 0, b[2] = 1", a[2], b[2]); 5/14

答 : static void WhatDoesThisDo(int[] x) x = new int[4] 8, 9, 0, 1 ; x[2] = 7; 6. 試寫出下列程式的輸出 (10%) using System; namespace MidTerm2011Problem6 class Program static void Main(string[] args) string pattern = "survey"; char[] p = pattern.tochararray(); string target = "surgery"; char[] t = target.tochararray(); int[,] c = DP(p, t); int n = t.length; int m = p.length; int i; int j; for (i = 0; i <= m; ++i) for (j = 0; j <= n; ++j) Console.Write(c[i, j] + "\t"); Console.WriteLine(); 6/14

static int[,] DP(char[] p, char[] t) int m = p.length; int n = t.length; int[,] c = new int[m+1, n+1]; int i; int j; for (j = 0; j <= n; ++j) c[0, j] = 0; for (i = 0; i <= m; ++i) c[i, 0] = i; for (i = 1; i <= m; ++i) for (j = 1; j <= n; ++j) c[i, j] = (p[i-1] == t[j-1])? c[i - 1, j - 1] : 1 + Minimum(c[i - 1, j], c[i, j - 1], c[i - 1, j - 1]); return c; static int Minimum(int x, int y, int z) if (x <= y && x <= z) return x; if (y <= x && y <= z) return y; return z; 7/14

答 : 7. /* * MidTerm2011Problem7 * skj, 4/21/2011 * 尋找英文文章中的 Semantic Words * 並計算其出現次數, 由高而低顯示 * * 主程式虛擬碼 * 1. 輸入文章中的文句, 以行為單位存成一維陣列 * 2. 假設儲存 semantic words 的陣列 * 是 semanticword * 對應的出現次數是陣列 freq * 3. 取得 semanticword, freq * 4. 將 semanticword 及 freq 依 freq 由高而低排序 * 5. 輸出 semanticword 及 freq * * 函式虛擬碼 ProcessArticle( 文章, semanticword, freq) * 1. 假定放入 semanticword 及 freq 的單字數為 nsw * 令其初值為 0 * 2. for 文章每一行文句 * * 2.1 取出單字構成陣列 word * 2.2 for 所有 word 中的單字 * * 2.2.1 if 單字是 stop words, continue * 2.2.2 if 單字不在 semanticword 中 * * 2.2.2.1 將單字新增入 semanticword * 2.2.2.2 遞增 nsw 8/14

* * 2.2.3 遞增對應的 freq 元素 * * * * 測試規劃 : Robert Frost 的詩 The Road Not Taken 之最後一段 : * * I shall be telling this with a sigh * Somewhere ages and ages hence: * Two roads diverged in a wood, and I * I took the one less traveled by, * And that has made all the difference * * 應輸出 ( 括弧內代表出現次數, 由高而低排列, * 同次數單字出現序未必與此處相同 ): * ages (2) * telling (1) * sigh (1) * roads (1) * diverged (1) * wood (1) * less (1) * traveled (1) * difference (1) */ using System; using System.Diagnostics; namespace MidTerm2011Problem7 class Program static void Main(string[] args) Debug.Assert(Test_1_OK()); string[] semanticword; 9/14

int[] freq; int isw; string[] line = InputArticle(); ProcessArticle(line, out semanticword, out freq); Array.Sort(freq, semanticword); Array.Reverse(semanticWord); Array.Reverse(freq); Console.WriteLine( "\n 上列文章的 Semantic words 及出現次數 "); for (isw = 0; isw < semanticword.length; ++isw) Console.WriteLine("0 (1)", semanticword[isw], freq[isw]); static bool Test_1_OK() string[] semanticword; int[] freq; string[] line = new string[5] "I shall be telling this with a sigh", "Somewhere ages and ages hence:", "Two roads diverged in a wood, and I-", "I took the one less traveled by,", "And that has made all the difference" ; ProcessArticle(line, out semanticword, out freq); string[] correctsw = "ages", "telling", "sigh", "roads", "diverged", "wood", "less", "traveled", "difference" ; int[] correctfreq = 2, 1, 1, 1, 1, 1, 1, 1, 1 ; int isw; int idx; bool result = (semanticword.length == 9 && freq.length == 9); 10/14

if (result) for (isw = 0; isw < semanticword.length; ++isw) idx = Array.IndexOf( semanticword, correctsw[isw]); result = result && (idx >= 0 && freq[idx] == correctfreq[isw]); return result; static void ProcessArticle( string[] line, out string[] semanticword, out int[] freq) const int MAX_N_SW = 100; // 假設之 semantic words 字數上限 string[] sw = new string[max_n_sw]; // semantic words // 的 local 暫時版本 int[] fr = new int[max_n_sw]; // freq 的 local 暫時版本 int isw; for (isw = 0; isw < MAX_N_SW; ++isw) sw[isw] = null; fr[isw] = 0; int nlines = line.length; int il; string[] word; int nw; int iw; 11/14

int nsw = 0; for (il = 0; il < nlines; ++il) word = ProcessLine(line[iL]); nw = word.length; for (iw = 0; iw < nw; ++iw) if (IsAStopWord(word[iW])) continue; isw = Array.IndexOf(sw, word[iw]); if (isw < 0) isw = nsw; sw[isw] = word[iw]; ++nsw; fr[isw]++; // 產生要回傳的版本 semanticword = new string[nsw]; freq = new int[nsw]; Array.Copy(sw, semanticword, nsw); Array.Copy(fr, freq, nsw); static string[] InputArticle() Console.Write(" 輸入行數 : "); int nlines = int.parse(console.readline()); string[] line = new string[nlines]; int il; for (il = 0; il < nlines; ++il) Console.WriteLine(" 輸入第 0 行 ", il+1); line[il] = Console.ReadLine(); return line; 12/14

static string[] ProcessLine(string line) char[] separator = ' ', ',', '.', ':', ';', '\'','\\', '"', '/', '`', '-', ' ', '(', ')', '[', ']', '', '', '!', '@', '#', '$', '%', '^', '&', '*', '+', '_', '=', '~', '<', '>', '?' ; string[] word = line.split(separator); // 將單字改為全小寫, 以便比對 stop words int iw; for (iw = 0; iw < word.length; ++iw) word[iw] = word[iw].tolower(); return word; static bool IsAStopWord(string word) // 不算完全, 但可接受的 stop words 列表 string[] stopword = "","a","able","about","across", "after","all","almost","also","am", "among","an","and","any","are", "as","at","be","because","been", "but","by","can","cannot","could", "dear","did","do","does","either", "else","ever","every","for","from", "get","gets", "got","had","has", "have","he","hence","her","hers", 13/14

"him","his","how","however","i", "if","in","into","is","it", "its","just","least","let","lets", "likely","made","make", "makes", "may", "me","might","most","must","my", "neither","no","nor","not","of", "off","often","on","one","only", "or","other","our","own","rather", "said","say","says","shall","she", "should","since","so","some","somewhere", "take","taken","takes","than","that", "the","their","them","then","there", "these","they","this","tis","to", "too","took", "twas","two","us", "want","wants","was","we","were", "what","when","where","which","while", "who","whom","why","will","with", "would","yet","you","your" ; bool result = ( Array.IndexOf(stopword, word) >= 0 ); return result; 14/14