精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰計算機程式 課程性質︰農 課程教師︰林達德 開課學院: 開課系所︰ 考試日期(年月日)︰ 考試時限(分鐘): 是否需發放獎勵金: (如未明確表示,則不予發放) 要謝 試題 : 【九十八學年度上學期】 第一題:(10分) 解釋名詞 (1) stack 堆疊的結構,符合LIFO(Last-in, first-out)的特性, 最後放入記憶體位置的資料會最先被取出。 2) function overloading 函式重載。╴為類似功能的函式提供一個統一的名稱,但是 根據參數列各數或型態的不同,而自動呼叫對應的函式。╴╴╴╴╴ (3) ASCII American Standard Code for Information Interchange,是基於拉丁 字母的一套電腦編碼系統,每一個字元與符號都有一對應的數字碼。 (4) variable scope 變數可視範圍。就是定義變數可被識別與使用的範圍,可 藉此將變數分為全域變數(Global variable),區域變數(Local variable),區塊變 數(Block variable)。╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴ (5) pass by reference 傳址。將變數記憶體位置直接傳入函式中,運算完值間將結果放 入同一記憶體位置中。╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴ 第二題:(10分) 下列敘述執行後變數A,B,C,D,E之值分別為何? int A=0, B=0, C=0, D=0, E=0; while( B <= 20 ) { A = A + 2; B = B + A; C++; D = B + C%2; E *= 2.0; } ─────────────────────────────────── 答案欄: A = ˍ10ˍ B = ˍ30ˍ C = ˍ5ˍ D = ˍ31ˍ E = ˍ0ˍ 第三題:(10分)   下列程式片段執行後的結果請列於答案欄中 for( int i=1; i<=6; i++ ) { for( int j=1; j<=10; j++ ) { if( i%3 == 0 ) cout << ‘%’; else cout << ‘=’; } cout << endl; } ─────────────────────────────────── 答案欄: ========== ========== %%%%%%%%%% ========== ========== %%%%%%%%%% 第四題:(10分)   請將下列程式在個人電腦上執行後之輸出仔細地填入右側答案欄中。 #include <iostream> using std::cout; using std::endl; int funct1(int a); int funct2(int a); int a = 0, b = 1; void main() { int count; for( count = 1; count <=5; ++count) { b += funct1(a+1) + 1; cout << “b = “ << b << endl; } } int funct1( int a ) { b = funct2( a+2 ) + 2; return b; } int funct2( int a ) { return(b+a); } 答案欄: b = 13 b = 37 b = 85 b = 181 b = 373 第六題:(10分)   請寫出一個程式計算出符合下式的最小n值,並將結果輸出至電腦螢幕。 ─────────────────────────────────── 答案欄:(空間不夠時請寫於背面) #include "iostream" using namespace std; int main() { int n = 0; //代表的次方項變數 int m = 1; //儲存的n次方後的運算結果 do { n = n+1; //次方加 m *=3; //3的n次方運算 }while( m<=30000); //如果的n次方<=30000,則繼續尋找;否則跳出迴圈 cout<<m; //顯示結果 system ("pause"); return 0; } 第七題:(10分)   請計算並列印出費朋納西序列(Fibonacci sequence)之前二十項,列印之每一行共有 五個數(合計四行)。   提示:費朋納西序列為 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,..... ─────────────────────────────────── 答案欄:(空間不夠時請寫於背面) #include "stdafx.h" #include "iostream" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int a = 0; int b = 1; int c; cout << a << "\t" << b << "\t"; //直接輸出前二項 for (int i=2; i<=19; i++){ c = a + b; //費氏數列核心 a = b; b = c; cout << c << "\t"; if(i%5==4) //依題目要求換行 cout << endl; } system("pause"); return 0; } 第八題:(10分) 請設計一個程式,其功能為找出整數1至N之間的質數,整數N之值由使用者輸入,找到的 質數請輸出至螢幕上。 ─────────────────────────────────── 答案欄:(空間不夠時請寫於背面) #include "stdafx.h" #include "iostream" #include "cmath" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int N, temp=0; cout << "請輸入範圍:"; cin >> N; //使用者輸入範圍 for (int i=2; i<=N; i++){ //檢查~N for (int j=2; j<=sqrt((double)i); j++){ //檢查是否可 //被其他數字可整除 if (i%j == 0){ temp++; //有整除情況,將暫存值增加 } } if (temp) //若暫存值不為零,表示有整除,非質數, //不輸出並且將暫存值歸零 temp = 0; else cout << i << " "; //暫存值為零,i為質數,輸出 } system("pause"); return 0; } 第九題:(10分)   指數函數ex 可以下式計算之        請你設計一個函式計算指數函數(至第20項),同時在主程式中呼叫此函式計算e1, e2, e3, e4,...,至e10,並將結果輸出至螢幕。 ─────────────────────────────────── 答案欄:(空間不夠時請寫於背面) #include "stdafx.h" #include "iostream" #include "iomanip" #include "cmath" using namespace std; double e(int x); int _tmain(int argc, _TCHAR* argv[]) { for (int i=1; i<=10; i++) cout << "e^" << i << " = "<< setprecision(10) <<e(i) << endl; //呼叫十次e函 數 system("pause"); return 0; } double e(int x) { double result = 1; double factorial = 1; for (int i=1; i<20; i++){ factorial *= i; //階乘 result += pow((double)x,i) / factorial; //e的指數次方公式 } return result; } 第十題:(10分) 若一個整數等於除了自己以外所有因數之總和,則稱之為perfect number。請你撰寫一個 函式PerfectNumber(long int Num)來判斷傳進去的參數是否為一perfect number。並設 計一個主程式呼叫這個PerfectNumber(long int Num)函式來判斷自1到10000有哪些整數 是perfect number,將它們顯示在螢幕上。 ─────────────────────────────────── 答案欄:(空間不夠時請寫於背面) #include "iostream" using namespace std; bool PerfectNumber(long int); //定義函式PerfectNumber int main() { for (int i=1 ; i<=10000 ; i++) //迴圈尋找到之間的perfect number { if (PerfectNumber(i) == true) //如果函式判斷放入的數為 //perfect number,則輸出至螢幕 cout<<i<<endl; } system("pause"); return 0; } bool PerfectNumber(long int n) { int m = 0; //儲存因數累加結果的變數 for (int j=1 ; j<n ; j++) //迴圈尋找到n之間,n的因數 { if (n%j==0) //如果j為n的因數,則將因數累加至m中 m = m + j ; } if (n == m)//如果因數和m 等於輸入的數n ,則回傳true,否則回傳false return true; else return false; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.115.224 jasonfghx:轉錄至看板 NTUCH-101HW 12/10 23:27