作者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