第一题:(10分,每一小题2分)
解释名词
(1)EOF (2)global variable (3)srand() (4)ASCII (5)UML
Answer
(1)EOF :EOF= End Of File
(2)global variable :全域变数,能在任一函式中使用的变数,通常写於main之前
(3)srand() :乱数表 括弧内输入乱数 如3,2,time()
(4)ASCII :编码标准
(5)UML :Unified Modeling language,统一标准模塑语言,由标准的图像表示程式的架构与资料
================================================================================================
第二题:(10分,每一格2分)
考虑以下程式片断:
if(i == j)
cout<<"1"<<endl;
else if ((i % j)<3)
cout<<"2"<<endl;
else if (i<(j-1))
cout<<"3"<<endl;
else
cout<<"4"<<endl;
cout<<"5"<<endl;
若i是9而j是4时,输出为何? Answer: 25
若i是4而j是9时,输出为何? Answer: 35
若i是5而j是6时,输出为何? Answer: 45
若i是5而j是9时,输出为何? Answer: 35
若i是0而j是0时,输出为何? Answer: 15
================================================================================================
第三题:(10分,每一格2分)
下列叙述执行後变数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;
}
Answer:
A=10 B=30 C=5 D=31 E=0
================================================================================================
第四题:(10分)
请将下列程式在个人电脑上执行後输出仔细地填入右侧答案栏中
#include<iostream>
using std::cout;
using std::endl;
int funct1(int a);
int funct2(int a);
int a=0,b=1;
main()
{
int count;
for(count = 1;count <= 5;++count)
{
b += funct(a+1) + 1;
cout<<"b = "<<b<<endl;
}
}
int funct1(int a)
{
b = funct2( a+1 ) + 1;
return b;
}
int funct2(int a)
{
return(b+a);
}
Answer:跑不出来(假设b += funct(a+1) + 1;这里错误)
(更正成b += funct1(a+1) + 1;)
Answer:
b = 9
b = 25
b = 57
b = 121
b = 249
(更正成b += funct2(a+1) + 1;)
Answer:
b = 4
b = 10
b = 22
b = 46
b = 94
================================================================================================
第五题:(10分)
设计一个程式,在萤幕上显示下列的输出
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
Answer:
#include "stdafx.h" // 呼叫函式库
#include "iostream" // 呼叫iostream;有显示cout及输入cin的功能
using namespace std; // 开启所有stdafx的所有功能
int main()
{
int i,j;
for(i=1;i<7;i++)
{
for(j=0;j<i;j++)
{
cout<<j<<" ";
}
cout<<"\n";
}
system("pause"); //冻结画面
return 0; //回传一个值给main
}
================================================================================================
第六题:(10分)
请分别用 for,do while,while三种回圈的程式语法撰写程式,输出下列序列:
1,4,7,10,13,16,19,22,25,28
Answer:
#include "stdafx.h" // 呼叫函式库
#include "iostream" // 呼叫iostream;有显示cout及输入cin的功能
#include "iomanip" // 呼叫iomanip ;可以使用格式操作子
using namespace std; // 开启所有stdafx的所有功能
int main()
{
int i;
//==for==============================
cout<<"==for=============================="<<endl;
for(i=1;i<=10;i++)
{
cout<<3*i-2<<" ";
}
cout<<endl;
//==do while==============================
cout<<"==do while=============================="<<endl;
int j=1;
do
{
cout<<3*j-2<<" ";
j++;
}while(j <= 10);
cout<<endl;
//==while==============================
cout<<"==while=============================="<<endl;
int k=1;
while(k<=10)
{
cout<<3*k-2<<" ";
k++;
}
cout<<endl;
//================================
system("pause"); //冻结画面
return 0; //回传一个值给main
}
================================================================================================
第七题:(10分)
指数函数e^x可以下式计算之
x^2 x^3 x^4
e^x = 1 + x + ╴╴╴ + ╴╴╴ + ╴╴╴ +......
2! 3! 4!
请你设计一个函式计算指数函数(至第20项),同时在主程式中呼叫此函式计算
e^1,e^2,e^3,e^4,.....,至e^10,并将结果输出至萤幕上。
Answer:
#include "stdafx.h" // 呼叫函式库
#include "iostream" // 呼叫iostream;有显示cout及输入cin的功能
#include "iomanip" // 呼叫iomanip ;可以使用格式操作子
#include "cmath" // 呼叫标准数学函式库;
using namespace std; // 开启所有stdafx的所有功能
int Factorial(int);
double EXP(double);
int main()
{
int i;
for(i=1;i<=20;i++)
{
cout<<"x="<<setw(3)<<i<<setw(25)<<EXP(i)<<endl;
}
system("pause"); //冻结画面
return 0; //回传一个值给main
}
int Factorial(int N)
{
if(N <= 1)
return 1;
else
return N * Factorial(N-1); //呼叫自己
}
double EXP(double x)
{
int i;
double sum=1;
for(i=1;i<20;i++)
sum+=(pow(x,i))/Factorial(i);
return sum;
}
================================================================================================
第八题:(10分)
请设计一个程式,其功能为找出整数1至N间的质数,整数N之值由使用者输入,找到的质数
请输出至萤幕上。
Answer:
第九题:(10分)
请设计一个名为Sphere的类别,此类别有三个内部资料(Private data number),分别为
radius、volume与s_area。此类别中另有三个成员函式(Public mumber function),
分别为CalculateVolume、CalculateSurfaceArea与PrintSphere,用来计算球体的体积、
表面积与输出体积及表面积至电脑萤幕。
请写出这个类别的完整内容,包含成员函式的定义程式码。
[Hint:球体表面积公式为4πr^2,球体体积公式为(4/3)πr^3]
Answer:
第十题:(10分)
请写出一个程式计算丢掷五枚铜板出现正面次数的机率。此程式必须以主程式main()呼叫
函式 int five_coins()的方式完成,而 int five_coins()函式所回传给主程式的整数为
随机模拟丢掷五枚铜板後出现正面的铜板数。主程式则需要将每次的节果累计,最後将模
拟丢掷50000次铜板後,出现铜板正面为四个以上的机率输出到萤幕上。
[Hint:你将可能会使用到 srand()及rand()函式]
Answer:
#include "stdafx.h" // 呼叫函式库
#include "iostream" // 呼叫iostream;有显示cout及输入cin的功能
#include "iomanip" // 呼叫iomanip ;可以使用格式操作子
#include "ctime" // 呼叫标准时间函式库;
using namespace std; // 开启所有stdafx的所有功能
int five_coins(int);
int main()
{
srand(int(time(0)));
int i;
double add=0;
for(i=0;i<50000;i++)
{
if(five_coins(2)>=4)
{
add++;
}
}
cout<<add/500<<"%"<<endl;
system("pause"); //冻结画面
return 0; //回传一个值给main
}
int five_coins(int N)
{
int i,sum=0;
for(i=0;i<5;i++) sum+=rand()%N;
return sum;
}
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.36.226.196
1F:推 sdg85732:谢罗! 对了!第九题我们还没交吧 11/25 21:52
2F:推 ceorl:类别是之後才会教的 理论上不会考 11/25 22:02
※ 编辑: w86083 来自: 114.36.226.196 (11/25 23:35)