作者happierway (杰夫)
看板NTUBIME100HW
標題[試題]C++答案
時間Mon Nov 19 23:12:06 2007
PS.因為BBS頁寬的關係
有些換行的地方要特別注意...
(尤其是程式碼比較長的那幾行)
第一題:(10分)
解釋名詞
(1) stack last-in, first-out(LIFO) data structures
(2) syntax error compiler-time error, code that violate language
rules(syntax)
(3) local variable variables declared in a function definition's body
(4) lvalue variable can be used on the left side of an assignment
operater
(5) public: indicate the members appears after this access specifier
can be called by others in the program
第二題:(10分)
下列敘述執行後變數A,B,C,D,E之值分別為何?
int A=0, B=10, C=20, D=10, E=40, i=0;
while( i <= 5 )
{
if( i > 2 )
A = A + 5;
else if( D >= 7 )
B = B + A;
else
C = D + 2*C;
D--;
i++;
}
E *= i;
───────────────────────────────────
答案欄:
A = ˍ15ˍ B = ˍ10ˍ C = ˍ20ˍ D = ˍ4ˍ E = ˍ240ˍ
───────────────────────────────────
第三題:(10分)
下列程式片段執行後的結果請列於答案欄中
for( int i=1; i<=6; i++ ) {
for( int j=1; j<=15; j++ ) {
if( i%3 == 0 )
cout << ‘*’;
else
cout << ‘+’;
}
cout << endl;
}
───────────────────────────────────
第四題:(10分)
假設x的值為22,y的值為4,z的值為8,請將下列邏輯運算式的值寫出來(請填入true或false)。
邏輯運算式
真假值 (true or false)
x + y >= z
True
y == (x-2*z-2)
True
6*z!=z
True
(x=y)==4
True
(( x > y ) ? 5 : 10) == 5
True
──────────────────────────────────
第五題:(10分)
請將下列程式在個人電腦上執行後之輸出仔細地填入右側答案欄中。
答案欄:
x = 20
x = 30
x = 20
x = 10
x = 5
x = 6
x = 10
x = 5
x = 6
#include <iostream>
using std::cout;
using std::endl;
void a(void);
void b(void);
int x = 10;
main()
{
int x=20, y=0;
cout << “ x = “ << x << endl;
{
int x=30;
cout << “ x = “ << x << endl;
}
cout << “ x = “ << x << endl;
a();
b();
a();
b();
}
void a( void )
{
cout << “ x = “ << x << endl;
}
void b( void )
{
int x = 5;
cout << “ x = “ << x << endl;
x++;
cout << “ x = “ << x << endl;
}
第六題:(10分)
請設計一個程式,列出九九乘法表,其格式如下:
1*1=1 1*2= 2 1*3= 3 1*4= 4 1*5= 5 1*6= 6 1*7= 7 1*8= 8 1*9= 9
2*1=2 2*2= 4 2*3= 6 2*4= 8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18
… …
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
───────────────────────────────────
答案欄:(空間不夠時請寫於背面)
#include <iostream>
using namespace std ;
void main()
{
for(int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
cout << i << "*" << j << "=" << i*j << "\t" ;
}
cout << endl ;
}
}
第七題:(10分)
請計算並列印出費朋納西序列(Fibonacci sequence)之前二十項,
請注意列印之每一行共有五個數(合計四行)。
提示:費朋納西序列為 1, 1, 2, 3, 5, 8, 13, 21, 34,.....
───────────────────────────────────
答案欄:(空間不夠時請寫於背面)
#include <iostream>
using namespace std ;
int fib(int num)
{
if(num < 1)
return -1 ; //假如輸入值為負或0 則回傳負值代表錯誤
else if(num == 1)
return 1 ;
else if(num == 2)
return 1 ; //上兩項是遞迴限制
else
return fib(num-1) + fib(num-2) ; //遞迴
}
void main()
{
for(int i=1;i<=num;i++)
{
cout << fib(i) << " " ; //輸出
if(i % 5 == 0)
cout << endl ;
}
}
第八題:(10分)
請設計一個程式模擬醉漢走路的結果。假設醉漢的行走只有前後左右四個方向,
而往四個方向行進的機率都一樣,令醉漢初始的位置座標為(x,y)=(0,0),
請利用程式模擬算出醉漢行走1000步以後,他所在的座標位置。
───────────────────────────────────
答案欄:(空間不夠時請寫於背面)
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std ;
void main()
{
srand(time(0)) ; //初始化隨機種子
int x= 0 , y=0 , vec ; //位置 x y 方向 vec
for(int i=0;i<1000;i++) //移動1000次
{
vec = rand() % 4 ; //隨機取得要走的方向
switch(vec) //判斷此步的方向
{
case 0:
x += 1 ;
break ;
case 1:
x -= 1 ;
break ;
case 2:
y += 1 ;
break ;
case 3:
y -=1 ;
break ;
default:
break ;
}
}
cout << "(" << x << "," << y << ")" ;
}
第九題:(10分)
請設計一個名稱為Sphere的類別,此類別有三個內部資料(private data member),
分別為radius、volume與s_area。
此類別中另有三個成員函式(public member function),
分別為CalculateVolume、CalculateSurfaceArea與PrintSphere,
用來計算球體的體積、表面積與輸出體積及表面積至電腦螢幕。
請寫出這個類別的完整內容,包括成員函式的定義程式碼。
【Hint: 球體表面積公式為4pr2 ,球體體積公式為(4/3)pr3】
───────────────────────────────────
答案欄:(空間不夠時請寫於背面)
#define PI 3.1415926
#include <stdlib.h>
#include <iostream>
#include <cmath>
using namespace std ;
class Sphere
{
public:
Sphere(double r); //constructor 需輸入此物件的半徑
{
radius = r ;
}
double CalculateVolume(void) ; //member function
double CalculateSurfaceArea(void) ;
void PrintSphere(void) ;
private:
double radius ; //data member
double volume ;
double s_area ;
};
double Sphere::CalculateSurfaceArea(void)
{
s_area = 4.0 * PI * pow(radius,2) ;
return s_area ;
}
double Sphere::CalculateVolume(void)
{
volume = 4.0 / 3.0 * PI * pow(radius,3) ;
return volume ;
}
void Sphere::PrintSphere()
{
cout << "Surface area:" << s_area << endl ;
cout << "Volume:" << volume << endl ;
}
第十題:(10分)
請寫出一個程式模擬國內樂透彩卷的開獎號碼。
此程式必須以主程式main()呼叫函式int loto_number()的方式完成,
而loto_number()函式所傳回給主程式的整數為隨機模擬1~42中的一個號碼。
主程式則需要連續呼叫函式多次以產生
一組7個數字的樂透彩號碼(六個號碼加上一個特別號),最後將結果顯示於螢幕上。
※請特別注意,程式需要檢查相同的號碼不能同時出現於一組樂透彩號碼中。
【Hint: 您將可能會使用到 srand()及rand()函式 】
───────────────────────────────────
答案欄:(空間不夠時請寫於背面)
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
#include <iostream>
using namespace std;
int loto_number(void) //函式隨機輸出數字 1~42
{
return rand() % 42 + 1 ;
}
void main()
{
int n1 = 0 ,n2 = 0 ,n3 = 0 ,n4 = 0 ,n5 = 0 ,n6 = 0 ,n7 = 0 ;
//七個號碼先設定為0
srand(time(0)) ; //設定亂數種子
n1 = loto_number() ; //第一個數字無重複問題 直接取得即可
n2 = loto_number() ;
//第二個數字開始 需要檢查是否與之前的數字相同
while(n2 == n1) //檢查 若是相同 則一直取到不同為止
{
n2 = loto_number() ;
}
n3 = loto_number() ; //依此類推
while(n3 == n1 || n3 == n2)
{
n3 = loto_number() ;
}
n4 = loto_number() ;
while(n4 == n1 || n4 == n2 || n4 == n3)
{
n4 = loto_number() ;
}
n5 = loto_number() ;
while(n5 == n1 || n5 == n2 || n5 == n3 || n5 == n4)
{
n5 = loto_number() ;
}
n6 = loto_number() ;
while(n6 == n1 || n6 == n2 || n6 == n3 || n6 == n4 || n6 == n5)
{
n6 = loto_number() ;
}
n7 = loto_number() ;
while(n7 == n1 || n7 == n2 || n7 == n3 || n7 == n4 ||
n7 == n5 || n7 == n6)
{
n7 = loto_number() ;
}
cout << n1 << "," << n2 << "," << n3 << "," << n4 << "," <<
n5 << "," << n6 << "," << n7 << endl ;
//輸出結果
system("pause") ;
}
--
ζ
ξ
長的越帥,責任越重 ○-
<(╯
難怪這一生我活著都沒什麼壓力.. ■)﹥
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.115.55.158
推 arthurkot:傑夫是好人 11/20 19:20
推 happierway:別隨便發我卡~尤其是最近...... 11/20 22:43
推 Jason0815:杰夫是帥哥 11/21 19:55
推 happierway:樓上真中肯!!(大心) 11/21 22:11
→ angelwing12:杰夫是壞人 11/21 23:14