您的位置:首页 > 其它

比较三个数的大小

2014-01-17 23:51 169 查看
Description:比较三个数的大小;

Input: 任意的三个数;

Output:按大小顺序排列;

Sample Input : 3 4 2

Sample Output: 2 2 4

Method:

      1-按照顺序比较:(A B) (A C) (B C),若前者大于后者就交换,否者不交换。最终按顺序输出;

      2-找出最大值和最小值,最终按顺序输出;

Code-1:
#include<stdio.h>
int main()
{
int a , b , c , d ;
sancf("%d%d%d",&a,&b,&c);

if( a > b )
{ d = a ; a = b ; b = d ; }
if( a > c )
{ d = a ; a = c ; b = d ; }
if( b > c )
{ d = b ; b = c ; c = b ; }

printf("%d\t%d\t%d\n",a,b,c);
}
Code-2
#include<stdio.h>
int main()
{
int a , b , c , x , y , z ;
sancf("%d%d%d",&a,&b,&c);
x = a ;
if( b < x )
x = b ;
if( c < x ) // x 为最小的值
x = c ;

z = a ;
if( b > z )
z = b ;
if( c > z ) // z 为最大的值
z = c ;

y = a + b + c - x - z ;

printf("%d\t%d\t%d\n",x,y,z);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息