tft每日頭條

 > 圖文

 > 海南專升本-C語言基礎算法複習2

海南專升本-C語言基礎算法複習2

圖文 更新时间:2024-08-02 23:09:53

奧賽數據:

6.求m和n的最大公約數和最小公倍數。

海南專升本-C語言基礎算法複習2(海南專升本-C語言基礎算法複習2)1

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); }

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关圖文资讯推荐

热门圖文资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved