《(中职)C语言程序设计练习题8及答案.doc》由会员分享,可在线阅读,更多相关《(中职)C语言程序设计练习题8及答案.doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、练习题及答案11. /从键盘输入3个整数,求出最大值和最小值法A int max(int a,int n)int max,i;max=a0;for(i=1;in;i+)if(maxai) max=ai;return max;int min(int b,int n)int min,i;min=b0;for(i=1;ibi) min=bi;return min;#includevoid main()int i,c3,da,xiao;printf(Enter three integers:);for(i=0;i3;i+) scanf(%d,&ci);da=max(c,3);xiao=min(c,3)
2、;printf(max=%d,min=%dn,da,xiao);法B#include void main() int x,i,max,min,n; printf(Enter n:); scanf(%d,&n);printf(Enter %d integers:,n);scanf(%d,&x); max=min=x; for(i=1; in;i+) scanf(%d,&x); if(maxx) min=x; printf(max=%d,min=%dn,max,min); 2./输入一个年份,判断是否为闰年int y(int year)if(year%4=0&year%100!=0)|year%4
3、00=0) return 1;return 0;#includevoid main()int year;printf(Enter year:);scanf(%d,&year);if(y(year)=1) printf(Yes!n);else printf(No!n);3./*编程序求: x (x1) y = 3 x/5 (1=x=10)*/double f(double x,double y)if(x1) y=x;else if(x10) y=3-x/5;else y=3*x-11;return y;#includevoid main()double x,y;printf(Enter x:);
4、scanf(%lf,&x);y=f(x,y);printf(y=%fn,y);4./求1-100的和int s(int m)int i,sum=0;for(i=1;i=m;i+) sum=sum+i;return sum;#includeint main(void)int n,sum;printf(Enter n:);scanf(%d,&n);sum=s(n);printf(sum=%dn,sum);5./输入一个数判断是否为素数int prime(int x)int i; if(x=0|x=1) return 0;for(i=2;ix;i+)if(x%i=0) return 0; retur
5、n 1;#includeint main()int n,i;printf(Enter n:);scanf(%d,&n);if(prime(n)=0) printf(No!n);else printf(Yes!n);return 0;6./求分数序列:2/1,3/2,5/3,8/5,13/8求前20项之和int s(int n)int i;double fenzi=2,fenmu=1,temp,sum;sum=fenzi/fenmu;for(i=2;i=n;i+)temp=fenzi;fenzi=fenzi+fenmu;fenmu=temp;sum=sum+fenzi/fenmu;return
6、sum;#includevoid main()int m;double he;printf(Enter m:);scanf(%d,&m);he=s(m);printf(sum=%fn,he);7./*打印输出下面的图案 * * * * */#include void pyramid(int n) int i,j;for (i=1; i0;j-) printf( );for(j=1;j=2*i-1;j+) printf(* ); printf(n); void main(void) int m;printf(Enter m:);scanf(%d,&m);pyramid(m);8./求1000以内
7、的所有素数int prime(int x)int i; if(x=1) return 0;for(i=3;ix;i+)if(x%i=0) return 0; return 1;#includeint main(void)int j,n;printf(Enter n:);scanf(%d,&n);for(j=1;j=n;j+)if(prime(j)=1) printf(%d ,j); return 0;9./将一个整数分解成1、质因子或本身之积。如:5 = 1*5,6 =1*2*3,8=1*2*2*2#includevoid main()int i,n;printf(Enter n:);scan
8、f(%d,&n);printf(%d=1,n);for(i=2;in) t=m;m=n;n=t;for(i=1;i=m;i+)if(m%i=0&n%i=0) gys=i;return gys;#includevoid main()int a,b,gys;printf(Enter two integers:);scanf(%d%d,&a,&b);gys=fun(a,b);printf(gys=%dn,gys);11. /冒泡法排列十个整数void range(int a,int n)int i,j,t;for(i=0;in-1;i+)for(j=0;jaj+1) t=aj;aj=aj+1;aj+
9、1=t;#includevoid main()int i,b10;printf(Enter ten integers:);for(i=0;i10;i+) scanf(%d,&bi);range(b,10);for(i=0;i10;i+) printf(%d ,bi);putchar(n);12. /选择法排列十个整数void rank(int a,int n)int i,j,t;for(i=0;in-1;i+)for(j=i+1;jaj)t=ai;ai=aj;aj=t; #includevoid main()int b10,i;printf(Enter ten integers:);for(i
10、=0;i10;i+) scanf(%d,&bi);rank(b,10);for(i=0;i10;i+)printf(%d ,bi);13./编程实现两个字符串的连接,把第二个字符串连接到第一个字符串中void contact(char s1,char s2)int i,j;for(i=0;s1i;i+) ;for(j=0;s2j;j+) s1i+j=s2j;s1i+j=0;#includevoid main()char str180,str280;printf(Enter str1:);gets(str1);printf(Enter str2:); gets(str2);contact(str
11、1,str2);puts(str1);14./把一个字符串拷贝到另外一个字符串中void copy(char s1,char s2)int i;for(i=0;s1i;i+)s2i=s1i;s2i=0;#includevoid main()int i;char str180,str280;printf(Enter a string:);gets(str1);copy(str1,str2);puts(str2);15./在10个数的数组中进行折半查找int half(int x,int a,int n)int high=n-1,low=0,mid;while(low=high)mid=(low+
12、high)/2;if(xamid) low=mid+1;else if(x=amid) break;else high=mid-1;if(low=mid) return mid;return 0;#includevoid main()int x,i,b10;printf(Enter ten integers:);for(i=0;i10;i+) scanf(%d,&bi);printf(Enter x:);scanf(%d,&x);if(half(x,b,10)=0) printf(Its not in the array!n);else printf(Its index is %d!,half
13、(x,b,10);16./编程实现包含10个数组元素的逆序存放void reverse(int a,int n)int i,j,t;for(i=0,j=n-1;ij;i+,j-)t=ai;ai=aj;aj=t;#includevoid main()int i,b10;printf(Enter ten integers:);for(i=0;i10;i+) scanf(%d,&bi);reverse(b,10);for(i=0;i10;i+) printf(%d ,bi);17./*打印下面的数字金字塔 1 121 12321 1234321 123454321 12345654321 12345
14、67654321 12345678765432112345678987654321*/#includevoid pyramid(int n)int i,j;for(i=1;i=n;i+)for(j=1;j=n-i;j+) printf( ); for(j=1;j0;j-) printf(%d,j);printf(n);void main()int n;printf(Enter n:);scanf(%d,&n);pyramid(n);18./在一个有序数组中插入一个新元素,保证它继续有序,数组的元素个数为11个,先存放10个有序的数void insert(int x,int a,int n)in
15、t i,j;for(i=0;in-1;i+)if(x=i;j-) aj=aj-1;ai=x;#includevoid main()int x,i,b11;printf(Enter ten arranged integers:);for(i=0;i10;i+) scanf(%d,&bi);printf(Enter x:);scanf(%d,&x);insert(x,b,11);for(i=0;i11;i+) printf(%d ,bi);19./打印除杨辉三角形前10行#includevoid main()int i,j,a1010=0;for(i=0;i10;i+)ai0=1;aii=1;fo
16、r(i=2;i10;i+)for(j=1;j10;j+)aij=ai-1j-1+ai-1j;for(i=0;i10;i+)for(j=0;j=i;j+)printf(%5d,aij);printf(n); 20./编程求出1000以内所有的完数int wanshu(int x)int i,sum=0;for(i=1;ix;i+)if(x%i=0) sum=sum+i;if(sum=x) return x;return 0;#includevoid main()int n,i;printf(Enter n:);scanf(%d,&n);for(i=1;i=n;i+)if(wanshu(i)!=0) printf(%d ,i);