lab_10_31

advertisement
实验 10.31
Lab4-Ex2
A right triangle can have sides that are all
integers. A set of three integer values for
the sides of a right triangle is called a
Pythagorean triple. These three sides
must satisfy the relationship that the sum
of the squares of two of the sides is equal
to the square of the hypotenuse. Find all
Pythagorean triples for side1, side2 and
hypotenuse all no larger than 500.
int main(){
int count = 0; // number of triples found
long int hypotenuseSquared; // hypotenuse squared
long int sidesSquared; // sum of squares of sides
cout << "Side 1\tSide 2\tSide3" << endl;
// side1 values range from 1 to 500
for ( long side1 = 1; side1 <= 500; side1++ ) {
for ( long side2 = side1; side2 <= 500; side2++ ) {
for ( long hypotenuse = side2; hypotenuse <= 500; hypotenuse++ ) {
hypotenuseSquared = hypotenuse * hypotenuse;
sidesSquared = side1 * side1 + side2 * side2;
if ( hypotenuseSquared == sidesSquared ) {
cout << side1 << '\t' << side2 << '\t' << hypotenuse << '\n';
count++; // update count
break;
} // end if
} // end for
} // end for
} // end for
cout << "A total of " << count << " triples were found." << endl;
return 0; // indicate successful termination
} // end main
Lab5_Ex1
• 分别用三个函数实现绘制正方形(square)、
菱形(diamond)、三角形(triangle),在main
函数中用循环结构嵌套switch结构实现对各
种图形函数的调用测试。
#include <iostream>
#include <cmath>
using namespace std;
void square( int, char ); // function prototype
void diamond( int, char ); // function prototype
void triangle ( int, char ); // function prototype
int main(){
int size;
char character; int shape;
char answer;
do{
cout << "Choose the shape to graph" << endl<< "1 for square" << endl
<< "2 for diamond" << endl<< "3 for triangle, " << endl << "4 for circle: " <<
endl << "? ";
cin >> shape; cout << "Enter a character and size: ";
cin >> character >> size; cout << '\n';
switch( shape ){
case 1: square( size, character ); break;
case 2: diamond( size, character ); break;
case 3: triangle( size, character );break;
default: square( size, character ); break;
} // end switch
cout<<"do you want to continue? ( y or n)"<<endl;
cin>>answer;
}while(answer=='y');
return 0; // indicate successful termination
} // end main
// square displays solid square of fillCharacter with
specified side
void square( int side, char fillCharacter )
{
// loop side times for number of rows
for ( int row = 1; row <= side; row++ )
{
// loop side times for number of columns
for ( int col = 1; col <= side; col++ )
cout << fillCharacter;
cout << endl;
} // end for
cout << endl;
} // end function square
void diamond( int size, char fillCharacter ){
int rows;
// top half
for ( rows =size%2; rows <= size - 2; rows += 2 ){
// print preceding spaces
for ( int space = ( size - rows) / 2; space > 0; space-- )
cout << ' ';
// print characters
for ( int character = 1; character <= rows; character++ )
cout << fillCharacter;
cout << endl;
} // end for
// bottom half
for ( rows = size; rows >= 0; rows -= 2 ){
// print preceding spaces
for ( int space = ( size - rows ) / 2; space > 0; space-- )
cout << ' ';
// print characters
for ( int character = 1; character <= rows; character++ )
cout << fillCharacter;
cout << endl;
} // end for
cout << endl;
} // end function diamond
// triangle displays solid triangle of fillCharacter
// with specified number of rows
void triangle( int size, char fillCharacter )
{
// display triangle
for ( int row = 1; row <= size; row++ )
{
// print preceding spaces
for ( int space = size - row; space > 0; space-- )
cout << ' ';
// print characters
for ( int character = 1; character < 2 * row;
character++ )
cout << fillCharacter;
cout << endl;
} // end for
cout << endl;
} // end function triangle
Lab6_Ex2
• 一个完全数是这样的一个整数,它的所有
因子(包含1但不包含该数本身)的和恰好
等于该数本身。举个例子,6是一个完全数,
因为6=1+2+3。编写一个函数perfect(bool
类型返回值),用它来判断该函数的形式参
数是否为一个完全数。在程序中调用该函
数,找出1至1000这个范围内所有的完全数
并打印出来。
#include <iostream>
using std::cout;
using std::endl;
bool isPerfect( int ); // function prototype
void printSum( int ); // function prototype
int main()
{
cout << "Perfect integers between 1 and 1000:" << endl;
// loop from 2 to 1000
for ( int j = 2; j <= 1000; j++ )
{
// if current integer is perfect
if ( isPerfect( j ) )
printSum( j ); // print it as sum of factors
} // end for
cout << endl;
return 0; // indicate successful termination
} // end main
// isPerfect returns true if value is perfect integer,
// i.e., if value is equal to sum of its factors
bool isPerfect( int value )
{
int factorSum = 1; // current sum of factors
// loop through possible factor values
for ( int i = 2; i <= value / 2; i++ )
{
// if i is factor
if ( value % i == 0 )
factorSum += i; // add to sum
} // end for
// return true if value is equal to sum of factors
return factorSum == value ? true : false;
} // end function isPerfect
void printSum( int value )
{
cout << value << " = 1";
// loop through possible factor values
for ( int i = 2; i <= value / 2; i++ )
{
// if i is factor
if ( value % i == 0 )
cout << " + " << i; // display as part of
sum
} // end for
cout << endl;
} // end function printSum
Lab6_Ex3
(质数问题)一个质数是这样的一个整数,它只能被
1和该数本身整除。举个例子,2,3,4,7是质数,
而4,6,8,9不是质数。
1.编写一个函数来判断一个数是否为质数。
2.在程序中调用这个函数,找出2至10000这个范围
内所有的质数并且打印出来。在你确认找出所有质
数之前你测试了多少次呢?
3.最初,你可能认为判断一个数是否为质数的测试次
数上限为n/2,但实际你需要的测试次数的上限只
是n的平方根。这是为什么呢?重新编写这个程序,
分别在这两种方式下运行,并观察出性能的改进。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
bool isPrime( int ); // function prototype
int main(){
int count = 1; // total number of primes found
cout << "The prime numbers from 1 to 10000 are:" << endl;
cout << setw( 6 ) << 2; // 2 is only even prime
for ( int loop = 3; loop < 10000; loop += 2 ) {
if ( isPrime( loop ) )
{
count++;
cout << setw( 6 ) << loop;
if ( count % 10 == 0 ) // new line after 10 values displayed
cout << '\n';
} // end if
} // end for
cout << endl << "Total of " << count << " prime numbers between 1 and
10000." << endl;
return 0; // indicate successful termination
} // end main
// isPrime returns true if n is prime
bool isPrime( int n )
{
// loop through possible factors
for ( int i = 2; i <= n / 2; i++ )
//for ( int i = 2; i <= static_cast< int >
( sqrt( static_cast< double > ( n ) ) ); i++ )
{
// if factor found, not prime
if ( n % i == 0 )
return false;
} // end for
return true;
} // end function prime
Lab6_Ex4
• 两个整型数的最大公约数(The greatest
common divisor, GCD)定义为最大的整除
这两个整数的数。编写gcd函数,返回两个
整型数的最大公约数。
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int gcd( int, int ); // function prototype
int main(){
int a; // first number
int b; // second number
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The greatest common divisor of "
<< a << " and " << b << " is ";
// find greatest common divisor of a and b
cout << gcd( a, b ) << endl;
return 0; // indicate successful termination
} // end main
int gcd( int x, int y )
{
int greatest = 1;
// loop from 2 to smaller of x and y
for ( int i = 2; i <= ( ( x < y ) ? x: y ); i++ )
{
// if current i divides both x and y
if ( x % i == 0 && y % i == 0 )
greatest = i;
} // end for
return greatest;
} // end function gcd
Download