作者sima (踩到狗尾巴)
看板NTUBIME96-HW
标题计程考古题答案
时间Wed Nov 19 12:31:46 2003
【第一题】
1. True
2. False
3. False
4. True
5. True
【第二题】
a = 8192
b = 30
【第三题】
******
******
******
******
******
******
******
******
******
【第四题】
b=9
b=25
b=57
b=121
b=249
【第五题】
// Problem 5
#include "stdafx.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void main(void)
{
float s = 0.0;
int n;
cout << "Please give the number of terms in the S series\n";
cin >> n; // Get the number of terms
for( int i=1; i<=n; i++) // Calculate the S series
{
s = s + 1.0/i/i;
}
cout << "S = " << s << endl; // Output the result
}
【第六题】
x=2
x=4
x=8
x=16
x=32
【第七题】
// Problem 7
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
void main(void)
{
for( int i=1; i<=9; i++)
{
for( int j=1; j<=9; j++)
{
cout << i << '*' << j << '=' << setw(2) << i*j << ' ';
}
cout << endl;
}
}
【第八题】
//Problem 8
#include <iostream>
#include "conio.h" // using for getch() .
using std::cout;
using std::cin;
using std::endl;
void find(int N); //自订的找出整数N之内所有质数的函式(不传回值).
void main()
{
int N;
cout << "本程式之功能是让使用者找出整数1至N之间的质数" << endl;
cout << "请输入 N = ";
cin >> N;
cout <<"====================================="
"====================================="
<< endl;
find(N); //呼叫自行定义的函式找出质数并且做cout.
cout << "\nPress any key to continue...\n";
getch(); //使画面暂留.
}
void find(int N)
{
int nflag=0; //nflag代表每个数的因数的数目.
for(int n=1;n<=N;n++)
{
for(int i=1;i<=n;i++)
{
if(n%i==0)
nflag++; //每当一个数被除余零时,其因数数目nflag加1.
}
if(nflag<=2) //if判断.若是nflag小於等於2,表其因数只有1与本//身,则其为质数.
cout << n << "\t"; //将结果输出并以一个tab间隔.
nflag=0; //将nflag归零,再做下一个数的计算.
}
}
【第九题】
//Problem 9
#include <iostream>
#include "conio.h" // using for getch().
#include <ctime> // using for time().
using std::cout;
using std::cin;
using std::endl;
int loto_number(); //自行定义的乐透随机选取函式.
int Num1, Num2, Num3, Num4, Num5, Num6, NumS;
//宣告七个整数型态,代表六个号码以及一个特别号.
void main()
{
srand( time(0) ); // randomize by time.
Num1=loto_number(); //开始产生六个号码以及特别号.
Num2=loto_number();
Num3=loto_number();
Num4=loto_number();
Num5=loto_number();
Num6=loto_number();
NumS=loto_number();
cout << "本期乐透彩开奖的六个号码是:\n" ;
printf("%d\t%d\t%d\t%d\t%d\t%d\n", Num1, Num2, Num3, Num4, Num5, Num6);
// %d表整数型态 , \t 表tab之跳脱字元。用cout输出亦可.
cout << "特别号是:" << NumS << endl << endl;
cout << "下次再会...";
getch(); //将画面停格.
}
int loto_number()
{
int number;
do //从1~42随机选取一个数字
number=1+ rand()%42;//若是和Num1~6或NumS重复,则重选一个.
while ( (number== Num1) || (number== Num2) ||(number== Num3) ||(number== Num4) ||(number== Num5) || (number== Num6) || (number== NumS));
return number; //将随机选出的数字传回
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.7.59
1F:→ gippholugan:好棒! 推 219.91.9.68 11/19
2F:→ kaidou848:推推 推 61.216.12.175 11/20