资源描述:
《c语言部分习题及答案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.求出分段函数:
2、x+1
3、x<0y=5x+10≤x≤105+cos(x)x>10#include#includevoidmain(){doublex,y;scanf("%lf",&x);if(x<0)y=fabs(x+1);elseif(x<=10)y=5*x+1;elsey=5+cos(x);printf("y=%lf",y);}2.找出200以内最大的能被17整除的数。#includevoidmain(){inti;for(i=200;i>0;i--)if(
4、i%17==0)break;printf("200以内能被17整除最大数为:%d",i);}3.输入两个正整数,求它们的最大公约数和最小公倍数。法一:#includevoidmain(){intx,y,t,a,i;scanf("%d,%d",&x,&y);if(x0;i--)if(x%i==0&&y%i==0)break;printf("最大公约数为:%d",i);a=x;i=2;while(a%y!=0){a=x*i;i++;}print
5、f("最小公倍数为:%d",a);}求最大公约数:{intx,y,t,q;scanf("%d,%d",&x,&y);if(xvoidmain(){inta,b,t,x,y;scanf("%d,%d",&a,&b);if(a6、公倍数为:%d,最大公约数为:%d",x,y);}4.求一分数序列2/1,3/2,5/3,8/5,13/8,21/13…的前n项之和。#includevoidmain(){intf1,f2,n,i;doublesum1,sum2;scanf("%d",&n);sum1=sum2=0;if(n%2==0){{f1=0;f2=1;for(i=1;i<=n/2;i++){f1=f1+f2;f2=f2+f1;sum1=sum1+(double)f2/f1;}}{f1=1;f2=1;for(i=1;i<=n
7、/2;i++){f1=f1+f2;f2=f2+f1;sum2=sum2+(double)f2/f1;}}sum1=sum1+sum2;}else{{f1=0;f2=1;for(i=1;i<=(n+1)/2;i++){f1=f1+f2;f2=f2+f1;sum1=sum1+(double)f2/f1;}}{f1=1;f2=1;for(i=1;i<=(n-1)/2;i++){f1=f1+f2;f2=f2+f1;sum2=sum2+(double)f2/f1;}}sum1=sum1+sum2;}printf("%lf",
8、sum1);}5.编程求#include#includeinta(int);voidmain(){inti,n;doublesum;sum=0;scanf("%d",&n);for(i=1;i<=n;i++)sum=sum+pow(-1,i-1)/(double)a(2*i-1);printf("sum=%lf",sum);}inta(intx){inti,t=1;for(i=1;i<=x;i++)t=t*i;return(t);}6.求出200以内的素数。#include9、io.h>#includevoidmain(){inti,m;doublek;for(m=1;m<=200;m+=2){k=sqrt((double)m);for(i=2;i<=k;i++)if(m%i==0)break;if(i>k)printf("%5d",m);}printf("");}7.编写程序,输出如下图形。****************#includevoidmain(){inti,j;for(i=1;i<=4;i++){for(j=1;j<=4-i;j++)pri
10、ntf("");for(j=1;j<=i;j++)printf("*");printf("");}for(i=1;i<=3;i++){for(j=1;j<=i;j++)printf("");for(j=1;j<=4-i;j++)printf("*");printf("");}}8.编写程序生成一个对角线元素为4,上三角元素