作者egggge (咪咪蛋)
看板NTUBIME102HW
标题Re: [转录][试题] 96上 林达德 计算机程式语言 期中考
时间Wed Nov 25 22:10:52 2009
※ 引述《w86083 (小可)》之铭言:
第一题:(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"
#include "cmath"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double i,j=1,k=0,l,temp;
for(l=1;l<=20;l++)
{
for(i=1;i<=20; i++)
{
j=j*i;
k=k+pow(l,i)/j;
}
k=k+1;
temp=k;
cout<<temp<<endl;
k=0;
j=1;
}
system("pause");
return 0;
}
第八题:(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 <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int five_coins();
int main()
{
srand(time(NULL));
int i;
float sum=0;
for(i=0;i<50000;i++)
{
if(five_coins() >= 4)
sum++;
}
cout << "机率为: " << setprecision(3) << fixed
<< 100*sum/50000.0 << "%\n";
system("pause");
return 0;
}
int five_coins()
{
int i,coin,tim;
tim = 0;
for(i=0;i<5;i++)
{
coin = rand()%2;
if(coin)
tim++;
}
return tim;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.36.226.196
1F:推 sdg85732:谢罗! 对了!第九题我们还没交吧 11/25 21:52
2F:推 ceorl:类别是之後才会教的 理论上不会考 11/25 22:02
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.242.253
※ 编辑: egggge 来自: 140.112.242.253 (11/25 22:12)
3F:推 lldavuull:感谢解答^^ 11/26 02:50