《高级语言程序设计C++》平时作业(共14页).doc
精选优质文档-倾情为你奉上一、分析程序,写输出结果1 #include<iostream.h>#include<math.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 << " " 7 92 #include<iostream.h>void fun();void main()int i; for(i=1;i<5;i+) fun(); cout<<endl;void fun() static int a; int b=2; a += 2 ; cout<< a+b <<'t'46 8 10 3 #include<iostream.h>int fun(int n) if(n=0) return 1; return 2*fun(n-1);void main() int a=5; cout<<fun(a)<<endl;324 #include<iostream.h>void main() char *cp="word" for (int i=0 ; i<4; i+ ) cout<<cp+i << 't' word ord rd d二、根据程序功能填空。1. 程序把10个数存储到一维数组a中,并求该数组中最大值。#include<iostream.h>void main() int max; int a10=76,55,95,87,85,83,65,90,77,85; int *p=a ; max=*p; for( ; p< &a10 ; p+) if( *p>max ) max= *p ; cout<<"max= "<<max<<endl;2下面程序的功能是输出1至100之间每位数字的乘积大于每位数的和的数。例如,45两位数字的乘积为4×5=20,和为4+5=9。#include<iostream.h>void main() int n, k=1, s=0, m; for(n=1; n<=100; n+) k=1; s=0; 1m=n ;while( 2m ) k*=m%10; s+=m%10; 3 m/=10 ; if(k>s) cout<<n<<'t' 3程序对输入的n求s = 1 + 1/23 + 1/33 + + 1/n3 。#include<iostream.h>void main()double s; int i, n; cout<<" n= " cin>>n; s = 0; for (i=1; i<n ; i+) s= s=s+(1.0/(n*n*n) ; cout<<"s="<<s<<endl;4函数create从键盘输入整数序列,以输入0为结束。按输入顺序建立一个以head为表头的单向链表。struct nodeint data; node * next;create( node *head )node *p, *q; p=new node; cin>>p->data; q=p; while( p->data ) if(head=NULL) head=p; else q->next=p; ; q=p; p=new node; ; cin>>p->data; q->next=NULL; delete p; 5以下程序求方程的全部整数解:3x + 2y - 7z = 5( 0 x, y, z 100 )#include<iostream.h>void main() int x, y, z ; for( x=0; x<=100; x+ ) for( y=0; y<=100; y+ ) if( ( z=3*x+2*y-5 ) % 7 ) break ; z=3*x+2*y-5)/7 ;/求出z的值 if( z<=100&&z>=0 ) /检查z的范围 cout << "x=" << x << " y=" << y << " z=" << z << endl ; 三、程序设计1. 编写函数输出以下形状的图形,其中构成图形的数字和输出的行数通过参数传送。12 2 23 3 3 3 34 4 4 4 4 4 4#include<iostream.h>void main()int i,n;for (i=0;i<5; i+)for (n=0;n<2*i-1;n+)cout<<i;cout<<endl;2. 请编程序,输入两个正整数啊a和b(a<b),输出a、b之间所有整数的因数(除1和本身)。每行输出数据不超过10个。例如,若输入a为6,b为8,则输出格式要求如下:the factors of 6 :2 3the factors of 7 :no factorthe factors of 8 :2 4#include <iostream>using namespace std;void printFactor(int i);void main() int numA,numB; cout<<"Input Two Numbers:"<<endl; cin>>numA>>numB; for (int i=numA;i<=numB;i+) printFactor(i); void printFactor(int i)cout<<" FACTORS OF "<<i<<":"<<endl; int *fact=new int(); int n=0; for (int k=2;k<i;k+) if (i%k=0) factn=k; n+; if (n=0) cout<<"NO FACTOR"<<endl; else for (int j=0;j<n;j+) cout<<factj<<" " if (j+1)%10=0) cout<<endl; cout<<endl; 3请编程序,找出1至99之间的全部同构数。同构数是这样一组数:它出现在平方数的右边。例如:5是25右边的数,25是625右边的数,所以5和25都是同构数。#include<iostream>using namespace std;int main() long x,y,i=10; int flag=0; for(x=1;x<100;x+) y=x*x; while(y/i!=0) if(y%i=x) flag=1; break; i=i*10; if(flag=1) cout<<x<<' '<<y<<endl; flag=0; i=10; return 0; 4. 编写一个程序,实现如下功能: (1)从键盘输入a op b。其中a, b为数值;op为字符,限制为+、-、*、/ 。 (2)调用函数count(op,a,b),计算表达式a op b的值。由主函数输出结果。#include <iostream>using namespace std;template<typename T>T COUNT(char op,T a,T b) switch(op) case '+':return (a+b);break; case '-':return (a-b);break; case '*':return (a*b);break; case '/':return (a/b);break; default:cout << "Error." << endl;break; int main()int a,b;char op; cin >> a >> op >> b; if( op = '/' && b = 0 ) cout << "Input Error." << endl; cout << "Result is " << COUNT(op,a,b) << endl; system("pause"); return 0; 5. 编写一个程序,实现如下功能:(1)输入k(<100)个整数到数组x100中;(2)计算k个数的平均值及大于平均值的元素个数。#include<iostream.h>float average(int ,int); int num(int ,int); void main() int k,i,x100;float ave; for(k=0;k<100;k+) cin>>i;if(i=0)break;xk=i; ave=average(x,k); cout<<"平均值"<<ave<<endl; cout<<"大于平均值的元素个数"<<num(x,k)<<endl; float average(int u,int k) int a,sum=0; for(a=0;a<k;a+) sum+=ua; return (float)sum/k; int num(int u,int k) int b,c,m=0;int ave=average(u,k);for(c=0;c<k;c+) if(uc>ave)m+; return m; 6. 定义函数void reversion(int ary,int size);逆置数组ary的元素。例如实参数组原来为 1,5,3,2,6,8,9,4 ,调用函数reversion后变成为 4,9,8,6,2,3,5,1 。void reversion(int ary,int size)int i;int temp;for(i=0;i<size/2;i+)temp = aryi;aryi = arysize-i;arysize-i = temp;7. 数组a包含50个整数,把a中所有的后项除以前项之商取整后存入数组b(即bi=ai/ai-1,并且b50=a20/a1),最后按每行5个元素的格式输出数组b。#include<iostream.h>void main() int a50,b50; int i,j=0;/j用来控制换行 for(i=1;i<=50;i+) cout<<"nInput a "<<i<<": " cin>>ai; for(i=1;i<=50;i+) if(i!=50) bi=ai/ai-1; else bi=a20/a1; for(i=0;i<=50;i+) if(j!=5) cout<<"n" cout<<" "<<bi; 8. 编程输出所有不超过100 的其平方具有对称性质的正整数(也称回文数)。输出格式如下:number square1 12 43 911 12122 48426 676#include<iostream.h>void main() int i; long text(int i);/判断i的i2是不是回文数的函数 for(i=4;i<=100;i+) if(text(i)!=0) cout<<"n "<<i<<" "<<text(i); long text(int i) long sum,k,sum1=0; sum=i*i; k=sum; while(k>0) sum1=sum1*10+k%10 ; k=k/10; if(sum1=sum) return i*i; else return 0;9. 编写程序,打印如下杨辉三角。11 1 1 2 11 3 3 11 4 6 4 1#include<stdio.h> void main() int a55,i,j; for(i=0;i<5;i+) for(j=5;j>=i;j-) printf("%2c",' ');/*两个空格*/ for(j=0;j<=i;j+) if(i=j|j=0) aij=1; else aij=ai-1j+ai-1j-1; printf("%3d ",aij); /*%3d后一个空格*/ if(i=j) printf("n"); 10定义一个函数,计算长度为k的整型数组元素的平均值及大于平均值的元素个数。调用函数的语句为:count(a,k,ave,num);其中a是数组名,k是数组元素个数,ave返回的平均值,num返回大于平均值的元素个数。void count(a,k,&ave,&num) int count = 0,sum = 0; int i; /求平均数 for(i = 0; i < a; i+) sum += ai; ave = (float)sum / (float)a; /求大于平均数的个数 for(i = 0; i < a; i+) if(ai > ave) count+; num = count;专心-专注-专业