文档详情

华工网络教育,《高级语言程序设计C++》作业..doc

发布:2017-01-25约7千字共14页下载文档
文本预览下载声明
一、分析程序,写输出结果 1. #includeiostream.h #includemath.h void main() {int m, k, i ; for( m=1; m=10; m+=2 ) { k = m/3; for( i=2; i=k; i++ ) if( m%i ) cout m ; } } 答:m 的取值为1,3,5,7,9 对应k的取值为0,1,1,2,3, 第二个for循环:只有当k=2和k=3时才执行, 当k=2,i=2,m%i等于1为真,输出m为7 当k=3,i=2,m%i等于1为真,输出m为9, i=3,m%i等于0为假,无输出 结果为: 7 9 2. #includeiostream.h void fun(); void main() {int i; for(i=1;i5;i++) fun(); coutendl; } void fun() { static int a; int b=2; a += 2 ; cout a+b \t; } 答:static变量只会第一次初始化为0,之后保留就是上一次的值 输出4 6 8 10 3. #includeiostream.h int fun(int n) { if(n==0) return 1; return 2*fun(n-1); } void main() { int a=5; coutfun(a)endl; } 答:fun是递归函数如果是0就返回1,如果不是0就返回2*f(n-1),展开就是求2的n次方 结果为: 32 4. #includeiostream.h void main() { char *cp=word; for (int i=0 ; i4; i++ ) coutcp+i \t; } 答:cp是指向word的字符指针cp+i表示每次cp之后第i个字符的指针cout表示输出字符指针只想的字符串,遇到’\0\’后停止 答案 word ord rd d 二、.根据程序功能填空。 1. 程序把10个数存储到一维数组a中,并求该数组中最大值。 #includeiostream.h void main() { int max; int a[10]={76,55,95,87,85,83,65,90,77,85}; int *p= a ; max=*p; for( ; p a[10] ; p++) if( *pmax ) max= *p ; coutmax= maxendl; } 2.下面程序的功能是输出1至100之间每位数字的乘积大于每位数的和的数。例如,45两位数字的乘积为4×5=20,和为4+5=9。 #includeiostream.h void main() { int n, k=1, s=0, m; for(n=1; n=100; n++) { k=1; s=0; m =n ; while( m0 ) { k*=m%10; s+=m%10; m=m/10 ; } if(ks) coutn\t; } } 3.程序对输入的n求s = 1 + 1/23 + 1/33 + … + 1/n3 。 #includeiostream.h void main() {double s; int i, n; cout n= ; cinn; s = 0; for (i=1; in ; i++) s= s+(double)1/(i*i*i) ; couts=sendl; } 4.函数create从键盘输入整数序列,以输入0为结束。按输入顺序建立一个以head为表头的单向链表。 struct node{int data; node * next;}; create( node *head ) {node *p, *q; p=new node; cinp-data; q=p; while( p-data
显示全部
相似文档