资源描述:
《C语言第七章习题答案.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、7.1写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输入。hcf(u,v)intu,v;{inta,b,t,r; if(u>v) {t=u;u=v;v=t;} a=u;b=v; while((r=b%a)!=0) {b=a;a=r;} return(a);}lcd(u,v,h)intu,v,h;{intu,v,h,l; scanf("%d,%d",&u,&v); h=hcf(u,v); printf("H.C.F=%dn",h); l=lcd(u,v,h); printf("L.C.d=%dn",
2、l); return(u*v/h);} #includevoid main(){intu,v,h,l; scanf("%d,%d",&u,&v); h=hcf(u,v); printf("H.C.F=%dn",h); l=lcd(u,v,h); printf("L.C.D=%dn",l);} 7.2求方程根floatx1,x2,disc,p,q;greater_than_zero(a,b)floata,b;{ x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a);}equal_to_ze
3、ro(a,b)floata,b;{x1=x2=(-b)/(2*a);} smaller_than_zero(a,b)floata,b;{p=-b/(2*a); q=sqrt(disc)/(2*a);} main(){ floata,b,c; printf("n输入方程的系数a,b,c:n"); scanf("%f,%f,%f",&a,&b,&c); printf("n方程是:%5.2f*x*x+%5.2f*x+%5.2f=0n",a,b,c); disc=b*b-4*a*c; printf("方程的解是:n"); if(disc>0) {great_t
4、han_zero(a,b); printf("X1=%5.2ftX2=%5.2fnn",x1,x2); } elseif(disc==0) { zero(a,b); printf("X1=%5.2ftX2=%5.2fnn",x1,x2); } else { small_than_zero(a,b,c); printf("X1=%5.2f+%5.2fitX2=%5.2f-%2.2fin",p,q,p,q); }} 7.3写一个判断素数的函数,在主函数输入一个整数,输出是否是素数的信息。#includ
5、e#includevoid main(){intnumber; scanf("%d",&number); if(prime(number)) printf("yes"); else printf("no"); } intprime(number)intnumber;{intflag=1,n; for(n=2;n6、eN3intarray[N][N];convert(array)intarray[3][3];{inti,j,t; for(i=0;i7、组是:n"); for(i=0;i#includev
8、oid main(){ charstr[100]; printf("输入字符串: