} }
else cout << \你算错了!\ }
cout << \你的成绩是:\分\
int Rand(int m, int n) { static int r; do
{ r = ( 25173*r + 13849 ) % 65536 ;} while (r
//静态变量保留上一个随机数
5.已知勒让德多项式为
1??pn(x)??x?((2n?1)p(x)?(n?1)p(x))/nn?1n?2?n?0n?1n?1
编一程序,从键盘上输入x和n的值,使用递归函数求pn(x)的值。
解答:
该程序可以由递归函数的形式直接编写。
#include
cout << \ cin >> x >> n;
cout << \}
float p( float x,int n ) { float t1,t2;
if( n == 0 ) return 1;
else if( n == 1 ) return x; else
{
t1 = ( 2*n-1 )*p( x,n-1 ); t2 = ( n-1 )*p( x,n-2 ); cout << t1 << t2 << endl; return ( t1-t2 )/n;
21
}
}
6.把以下程序中的print()函数改写为等价的递归函数。
#include
void print( int w )
{ for( int i = 1 ; i <= w ; i ++ ) { for( int j = 1 ; j <= i ; j ++ ) cout << i << \ cout << endl ; } }
void main()
{ print( 5 ) ; } 运行显示: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
解答:
#include
void main() {
print( 5 ); }
{
for( i=1; i<=w; i++ ) cout << w << \ cout << endl; }
print( w-1 );
7.已知用梯形法求积分的公式为:Tn?h(f(a)?f(b))2n?1??i?1f(a?ih),其中
h = ( b-a )
/ n,n为积分区间的等分数,编程序求如下积分的值。要求把求积分公式编写成一个函数,
并使用函数指针作为形式参数。调用该函数时,给定不同的被积函数作为实际参数求不同的积分。
22
①
1?1?04x2dx ②
2??11?xdx2 ③
2?sin0xdx
解答:
#include
{ return 4 / ( 1 + x*x ); } double f2( double x )
{ return sqrt( 1 + x*x ); } double f3( double x ) { return sin( x ); }
double trap( double( *fun )( double x ), double a,double b,long n ) {
double t,h; int i;
t = ( ( *fun )(a) + ( *fun )( b ) ) / 2.0; h = ( b – a ) / n;
for( i=1; i<=n-1; i++ ) t += ( *fun )( a + i * h ); t *= h; return t; }
void main() {
double t1,t2,t3;
t1 = trap( f1,0,1,10000 ); cout << \ t2 = trap( f2,1,2,10000 ); cout << \
t3 = trap( sin,0,3.14159265/2,10000 ); cout << \}
8.编写一个程序,包含三个重载的display函数和一个主函数。要求第一个函数输出double值,前面用字符串“a double:”引导,第二个函数输出一个int值,前面用字符串“a int:”引导,第三个函数输出一个char字符值,前面用字符串“a char:”引导,在主函数中分别用double、int和char型变量作为实参调用display函数。
解答:
#include
{ cout << \
23
void display( int i )
{ cout << \void display( char c )
{ cout << \void main()
{ double d = 1.5; int i = 100; char c = 'a'; display( d ); display( i ); display( c ); }
9.使用重载函数编程序分别把两个数和三个数从大到小排列。 解答:略。
n10.给定求组合数公式为:cm?m!n!(m?n)!,编一程序,输入m和n的值,求cm的值。
n注意优化算法,降低溢出可能。要求主函数调用以下函数求组合数:
int Fabricate( int m, int n ) ; Fabricate函数内又须调用Multi函数: int Multi( int m, int n ) ;
// 返回 m × m-1 × … × n //返回cm的值
n程序由4个文件组成。头文件存放函数原型作为调用接口;其他3个cpp文件分别是
main、Fabricate和Multi函数的定义。
解答:
//Fabricate.h #ifndef FABRICATE_H #define FABRICATE_H
int Fabricate( int m,int n ); int Multi( int m, int n );
#endif
//main.cpp
#include
cout << \ cin >> m >> n;
cout << \}
24
//Fabricate.cpp #include \int Fabricate( int m, int n ) {
return Multi( m, m –n + 1 ) / Multi( n, 1 ); }
//Multi.cpp
int Multi( int m, int n ) {int i, t = 1;
for( i=n; i<=m; i++ ) t = t * i; return t; }
第4章 数 组
4.1 选择题
1.以下对一维数组 a 的正确定义是( c )。
(a) int n = 5, a[n]; (b) int a(5);
(c) const int n = 5; int a[n]; (d) int n; cin>>n; int a[n]; 2.下列数组定义语句中,不合法的是( a )。
(a) int a[3] = { 0, 1, 2, 3 }; (b) int a[] = { 0, 1, 2 }; (c) int a[3] = { 0, 1, 2 }; (d) int a[3] = { 0 };
3.已知 int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, *p = a ;则不能表示数组 a 中元素的式子是( c )。
(a) *a (b) *p (c) a (d) a[ p-a ] 4.已知 int a[] = { 0, 2, 4, 6, 8, 10 }, *p = a ; 值不等于0的表达式是( b,d )。 (a) *(p++) (b) *(++p) (c) *(p-- ) (d) *(--p) 5.以下不能对二维数组a进行正确初始化的语句是( c )。 (a) int a[2][3] = { 0 };
(b) int a[][3] = { { 0, 1 }, { 0 } };
(c) int a[2][3] = { { 0, 1 }, { 2, 3 }, { 4, 5 } }; (d) int a[][3] = { 0, 1, 2, 3, 4, 5 };
6.已知int a[][3] = { { 0, 1 }, { 2, 3, 4 }, { 5, 6 }, { 7 } } ;则 a[2][1]的值是( c )。
(a) 0 (b) 2 (c) 6 (d) 7
7.已知int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; 则不能表示数组元素a[2][1]
25