='A' &&" />
当前位置:文档之家› C 语言 求任意两个数的最大值 分支

C 语言 求任意两个数的最大值 分支

1. 输入一个字符,若是字母则输出“YES !”(JP48-6)main (){ char ch;clrscr();printf("enter char ch=?");scanf("%c",&ch);if (ch>='A' && ch<='Z' || ch>='a' && ch<='z')printf("yes !");}2.求任意两个数的最大值。

(分支程序) (例1-3-151)main(){ int a,b,c;clrscr();printf("a,b=?");scanf("%d,%d",&a,&b); /* 从键盘上临时输入a,b 的值 */ if (a>b) c=a; /* 调用求最大值的函数 max */else c=b;printf("max=%d\n",c); /* 输出 a 和 b 的最大值 c */}3. 调用求最大值的函数,求任意两个数的最大值。

(例1-3-152)main(){ int a,b,c;clrscr();printf("a,b=?");scanf("%d,%d",&a,&b); /* 从键盘上临时输入a,b 的值 */ c=max(a,b); /* 调用求最大值的函数 max */ printf("max=%d\n",c); /* 输出 a 和 b 的最大值 c */ }int max(x,y) /* 定义函数 max 为整型 */int x,y; /* 定义形式参数 x,y为整型 */{ int c;if (x>y) c=x; /* 求 x,y 的最大值 */else c=y;return (c);}4.求三个数中的最大数(4-3-1325)main(){ float a,b,c;clrscr();printf(“enter float a,b,c=?”);scanf(“%f,%f,%f”,&a,&b,&c);if (a>b)if (a>c) printf(“a=%f is the largest number !”,a);else printf(“c=%f is the largest number !”,c); /*a>b的情况下*/ else if (b>c) printf(“b=%f is the largest number !”,b); /*b>a的情况下*/ else printf(“c=%f is the largest number !”,c);}5.三个数从小到大排序运行该程序:Ctrl F9main () (4-3-13242)x,y,z=?3,-5,9{ float x,y,z,t; x=3.000000 y=-5.000000 z=9.000000printf("x,y,z=?"); -5.000000 3.000000 9.000000scanf("%f,%f,%f",&x,&y,&z);printf("x=%f\ty=%f\tz=%f\n",x,y,z);if (x>y){ t=x; x=y; y=t;}if (y<z) printf("%f\t%f\t%f\n",x,y,z);else if (x<z) printf("%f\t%f\t%f\n",x,z,y);elseprintf("%f\t%f\t%f\n",z,x,y);}6.三个数从大到小排序。

运行该程序:Ctrl F9main () (4-3-13241)x,y,z=?3,-5,9{ float x,y,z,t; x=3.000000 y=-5.000000 z=9.000000printf("x,y,z=?"); 9.000000 3.000000 -5.000000scanf("%f,%f,%f",&x,&y,&z);printf("x=%f\ty=%f\tz=%f\n",x,y,z);if (x<y) {t=x; x=y; y=t;} /* x>y */if (y>z) printf("%f\t%f\t%f\n",x,y,z); /*在x>y */else if (x>z) printf("%f\t%f\t%f\n",x,z,y); /* 在y最小*/elseprintf("%f\t%f\t%f\n",z,x,y); }7.输入三个数,将它们从大到小排序(4-3-333)main () Ctrl F9{ float a,b,c,max,min,m; clrscr(); a,b,c=?-10,9,6printf("a,b,c=?"); a=-10.000000 b=9.000000 c=6.000000scanf("%f,%f,%f",&a,&b,&c); 9.000000 6.000000 -10.000000printf(“a=%f\tb=%f\tc=%f\n”,a,b,c);max=(a>b)?a:b;max=(max>c)?max:c;min=(a<b)?a:b;min=(min<c)?min:c;m=a+b+c-max-min;printf("%f\t%f\t%f\n",max,m,min);}8.根据学生的成绩score,按分数分段评定等级“A ~E”如果分数 < 0或分数>100则输出“输入的成绩有错”的信息。

(4-3-1321)main(){ float score;clrscr();printf(“enter float score=?”);scanf(“%f”,&score);if (score>100 || score<0) printf(“ENTER score ERROR !\n” )else if (score>=90) printf(“score=%d\t\tA\n”,score);else if (score>=80) printf(“score=%d\t\tB\n”,score);else if (score>=70) printf(“score=%d\t\tC\n”,score);else if (score>=60) printf(“score=%d\t\tD\n”,score);else printf(“score=%d\t\tE\n”,score);}9.100制转换为五级记分制: (4-4-123)main (){ int x,sn;printf("int x (0--100) =?");scanf("%d",&x);if (x<0 || x>100) printf(“data error!\n”)else if (x<60) sn=0else{ sn=x/10-5;switch (sn){ case 1 : printf("x=%d\t pass\n",x);break;case 2 : printf("x=%d\t good\n",x);break;case 3 : printf("x=%d\t better\n",x);break;case 4 : printf("x=%d\t best\n",x);break;case 5 : printf("x=%d\t best\n",x);break;default : printf("x=%d\t fail\n",x);}}}10. 下面程序找出数组a中的最小元素。

main ( ){ int a [ ] = { 4,3,1 ,-7,10 ,12 , -2 , 6 , 9 , 25 }int i , min ;min = _____ a[0] ______ ;for ( i = 1 ; i < 10 ; i + + ) if ( ____ a[i]<min ___ ) min = a [ i ] ;printf ( “ min data is %d \n” , min ) ;}11. 下面程序找出数组a中的最大元素。

main ( ){ int a [ ] = { 4,3,1 ,-7,10 ,12 , -2 , 6 , 9 , 25 }int i , max ;max = _____a[0]______ ;for ( i = 1 ; i < 10 ; i + + ) if ( _a[i]>max____ ) max = a [ i ] ;printf ( “ m ax data is %d \n” , m ax ) ;}。

相关主题