作者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
1F:推 arthurkot:杰夫是好人 11/20 19:20
2F:推 happierway:别随便发我卡~尤其是最近...... 11/20 22:43
3F:推 Jason0815:杰夫是帅哥 11/21 19:55
4F:推 happierway:楼上真中肯!!(大心) 11/21 22:11
5F:→ angelwing12:杰夫是坏人 11/21 23:14