海南专升本-C语言基础算法复习2

奥赛数据:

6.求m和n的最大公约数和最小公倍数。

海南专升本-C语言基础算法复习2

main() {int m,t,n,i=2,yueshu,beishu; scanf("%d%d",&m,&n); if (m<=n) 存放 大数, 存放小数 {t=m;m=n;m=t;} /*m n */ 最大公约数 for (i=n;i>1;i--) /* */ { if (m%i==0 && n%i==0) {yueshu =i; break;} }

最小公倍数 beishu= n*m/yueshu; /* */ printf("%d he %d de zui da gong yue shu shi %d\n",m,n,yueshu ) ; printf("%d he %d de zui xiao gong bei shu shi %d\n",m,n,beishu ) ; } 7.求二元一次方程的根。 #include <math.h> main() { float a,b,c,x1,x2,deta; scanf("%f%f%f",&a,&b,&c); deta=b*b-4*a*c; if (deta>0) { x1=(-b+sqrt(deta))/(2*a); x2=(-b-sqrt(deta))/(2*a); printf("fangcheng de gen wei : x1=%f x2=%f",x1,x2) ; } else if (deta==0) {x1=x2=-b/(2*a); printf("fangcheng de gen wei : x1=x2=%f",x1);} else printf ("fangcheng mei you shi gen." ); }

8.符号函数。 main() { float x; int y; scanf(“ %f ”,&x); if (x>=0) if (x>0) y=1; else y=0; else y=-1; printf(“%f”,y); }9.函数x (x 1)、2x 1 (1 x 10) 、3x 11 (x 10) 输入x输出y。 main() {float x,y; scanf("%f",&x); if (x<1)

y=x; else if (x>=1 && x<10) y=2*x-1; else y=3*x-11; printf("%f",y); } 10.给出一个百分制成绩,要求输出成绩等级ABCDE ,90分以上为A ,80-89分为B,70-79分为C,60—69 分为D ,60分以下为E。 main() {int select,score; char grade; printf("Please input the score:"); scanf("%d\n",&score);select=score/10; switch(select) {case 1: case 2: case 3: case 4: case 5: grade= 'E'; break; case 6: grade= ‘D’; break; case 7: grade= ‘C’; break; case 8: grade=’B’; break; case 9: case 10: grade= ’A’; break; }/*注意使用break语句/* break */ printf("grade=%c\n",grade); } 11.求Fibonacci数列40个数 #include <stdio.h> main( ) { int n,i,un1,un2,un; 设置递推初始值 un = un2 = 1; /* for ( i=3; i<=40; i++) */ 用递推法计算第 项的值 /* N*/ { un1 = un2; un2 = un; un = un1 + un2; printf ("No. %d is %d\n", n, un); }