C+++习题及其解答(第3版).doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《C+++习题及其解答(第3版).doc》由会员分享,可在线阅读,更多相关《C+++习题及其解答(第3版).doc(111页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、9.在100到200中找出同时满足用3除余2,用5除余3和用7除余2的所有整数。【解答】#includeusing namespace std;int main() int i; for( i=100; i=200; i+ ) if ( ( i % 3 = 2) & ( i % 5 = 3 ) & ( i % 7 = 2 ) ) cout i endl; 10.求100到999中的水仙花数。所谓水仙花数是指一个三位数,它的每位数字的立方之和等于该数。例如,因为153=1+5+3,所以153为水仙花数。【解答】#includeusing namespace std;int main() int
2、i,a,b,c; for( i=100; i=999; i+ ) a = i/100; b = ( i-a*100 ) / 10; c = i - a*100 - b*10; if ( i = a*a*a + b*b*b + c*c*c ) cout i endl; 11.求1000之内的所有完数。所谓完数是指一个数恰好等于它的所有因子之和。例如,因为6=1+2+3,所以6为完数。【解答】#includeusing namespace std;int main() int i,j,s; for( i=1; i=1000; i+ ) s = 0; for( j=1; ji; j+ ) if (
3、i % j = 0 ) s = s + j; if ( i = s ) cout i endl; 12.编一程序显示由符号组成的三角形图案。例如,程序运行后, 屏幕显示:How many lines ?用户输入:5屏幕显示:What character ?用户输入:*则输出如下图案。* * * * * * * * * * * * * * * * * * * * *【解答】#includeusing namespace std;int main() int i,j,k,n; char ch; coutn; coutch; for( i=1; i=n; i+ ) for( k=1; k=n-i;
4、k+ ) cout ; for( j=1; j=2*i-1; j+ ) cout ch ; cout endl; 13.已知XYZ+YZZ=532,其中X,Y和Z为数字,编一程序求出X,Y和Z的值。【解答】#includeusing namespace std;int main() int x,y,z,i;for( x=1; x=9; x+ ) for( y=1; y=9; y+ ) for( z=0; z=9; z+ ) i = 100*x + 10*y + z + 100*y + 10*z + z; if ( i = 532 ) coutx=xty=ytz=zendl; 习题3及其解答3.
5、1 选择题1以下正确的函数原型为( d )。(a) f1( int x; int y ); (b) void f1( x, y );(c) void f1( int x, y ); (d) void f1( int, int );2有函数原型 void fun2( int ); 下面选项中,不正确的调用是( c )。(a) int a = 21; fun2( a );(b) int a = 15; fun2( a*3 ); (c) int b = 100; fun2( &b );(d) fun2( 256 );3有函数原型 void fun3( int * ); 下面选项中,正确的调用是( c
6、 )。(a) double x = 2.17; fun3( &x );(b) int a = 15 ; fun3( a*3.14 ); (c) int b = 100; fun3( &b );(d) fun3( 256 );4有函数原型 void fun4( int & ); 下面选项中,正确的调用是( c )。(a) int a = 2.17; fun4( &a );(b) int a = 15; fun4( a*3.14 );(c) int b = 100; fun4( b );(d) fun4( 256 ) ;5有声明 void fun5( int * & ); int a , *p =
7、 &a;下面选项中,正确的调用是( b )。(a) fun5( &a );(b) fun5( p );(c) fun5( *a );(d) fun5( *p ) ;6有声明 int fun6( int ),(*pf)(int) = fun6; 下面选项中,正确的调用是( c )。(a) int a=15; int n=fun6(&a); (b) int a = 15; cout(&pf)(a);(c) cout(*pf)( 256 ); (d) cout *pf( 256 );7在VC中,若定义一个函数的返回类型为void,以下叙述正确的是( c )。(a) 函数返回值需要强类型转换(b) 函
8、数不执行任何操作(c) 函数本身没有返回值(d) 函数不能修改实际参数的值8函数参数的默认值不允许为( c )。(a) 全局常量(b) 直接常量(c) 局部变量(d) 函数调用9使用重载函数编程序的目的是( a )。(a) 使用相同的函数名调用功能相似的函数(b) 共享程序代码(c) 提高程序的运行速度(d) 节省存贮空间10下列的描述中( b )是错误的。(a) 使用全局变量可以从被调用函数中获取多个操作结果(b) 局部变量可以初始化,若不初始化,则系统默认它的值为0(c) 当函数调用完后,静态局部变量的值不会消失(d) 全局变量若不初始化,则系统默认它的值为011下列选项中,( c )的具
9、有文件作用域。(a) 语句标号(b) 局部变量(c) 全局变量(d) 静态变量3.2 阅读下列程序,写出执行结果1#includeusing namespace std;#includeint f( int ) ;int main() int i; for( i = 0; i 3; i + ) cout f( i ) endl;int f( int a ) int b = 0 , c = 1; b +; c+; return int( a + pow( double(b), 2 ) + c );【解答】3452#includeusing namespace std;void func(int
10、a, int b, int c = 3, int d = 4 );int main() func( 10, 15, 20, 30 ); func( 10, 11, 12 ); func( 12, 12 );void func( int a, int b, int c, int d ) coutatbtctd endl; 【解答】10 15 20 3010 11 12 412 12 3 43#includeusing namespace std;void func( int, int, int * ) ;int main() int x, y, z; func( 5, 6, &x ); func
11、( 7, x, &y ); func( x, y, &z ); cout x , y , z endl;void func( int a , int b , int *c ) b += a ; *c = b - a ; 【解答】 6, 6, 64#includeusing namespace std;void func( int, int, int & );int main() int x=0 , y=1 , z=2; func( 1 , 2 , x ); func( x + y , y , y ); func( z , x + y , z ); cout x , y , z endl ;vo
12、id func( int a , int b , int &c ) b += a ; c = b - a ; 【解答】 2, 1, 35#includeusing namespace std;void func( int *, int *, int *& ) ;int main() int a=10, b=20; int *p=&a, *q=&b; func( p, q, p ); cout *p= *p ,*q= *q endl;void func( int *t1 , int *t2 , int *& rt ) *t1 += 5 ; *t2 += 5 ; rt = t1 ; *rt +=
13、5 ; cout *t1= *t1 ,*t2= *t2 ,*rt= *rt endl;【解答】*t1=20,*t2=25,*rt=20*p=20,*q=256#includeusing namespace std;int f2( int, int );int f1( int a , int b ) int c ; a += a ; b += b ; c = f2( a+b , b+1 ); return c;int f2( int a , int b ) int c ; c = b % 2 ; return a + c;int main() int a = 3 , b = 4; cout f1
14、( a , b ) endl;【解答】 157#includeusing namespace std;int age( int n ) int f; if( n = 1 ) f = 10 ; else f = age( n-1 ) + 2; return f ;int main() cout age : age( 5 ) endl; 【解答】 age:188#includeusing namespace std;int f1( int a, int b ) return a + b ; int f2( int a, int b ) return a - b ; int f3( int( *t
15、)( int, int ), int a, int b ) return ( *t )( a, b ) ; int main() int ( *p )( int, int ); p = f1 ; cout f3( p, 4, 8 ) endl; p = f2 ; cout f3( p, 8, 4 ) endl;【解答】 12 49#includeusing namespace std;int sub( int, int );int a = 1 ;int main() int m = 1, n = 2, f; f = sub( m, n ); cout a t f endl; f = sub(
16、m, n ) ; cout a t f endl;int sub( int c, int d ) static int m = 2, n = 5 ; cout m t n t endl; a = + a ; c = m + ; d = n +; return c + d ;【解答】2 52 73 63 93.3 思考题1函数的作用是什么?如何定义函数?什么叫函数原型?【解答】函数的两个重要作用:(1)任务划分,把一个复杂任务划分为若干小任务,便于分工处理和验证程序正确性;(2)软件重用,把一些功能相同或相近的程序段,独立编写成函数,让应用程序随时调用,而不需要编写雷同的代码。函数的定义形式:类
17、型 函数名 ( 形式参数表 ) 语句序列函数原型是函数声明,告诉编译器函数的接口信息:函数名、返回数据类型、接收的参数个数、参数类型和参数顺序,编译器根据函数原型检查函数调用的正确性。2什么叫函数值的返回类型?什么叫函数的类型?如何通过指向函数的指针调用一个已经定义的函数?请写一个验证程序说明。【解答】(1)函数的返回类型是函数返回的表达式的值得类型;(2)函数类型是指函数的接口,包括函数的参数定义和返回类型;(3)若有functionType functionName;/functionType是已经定义的函数类型functionType *functionPointer=functionN
18、ame;/定义函数指针并获取函数地址则可以通过函数指针调用函数:(*functionPointer)(argumentList);或functionPointer(argumentList);其中argumentList是实际参数表。验证程序:#includeusing namespace std;int main() typedef int myfunc(int,int); myfunc f,*fp; int a=10,b=6; fp=f; coutUsing f(a):f(a,b)endl;/函数名调用函数 coutUsing fp(a):fp(a,b)endl;/函数指针调用函数 cou
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 习题 及其 解答
![提示](https://www.deliwenku.com/images/bang_tan.gif)
限制150内