三个数求最大数、最小数、中间数

// 获取中间值

public static int getMidNum(int a, int b, int c)

 {

return (a < b ? (b < c ? b : a < c ? c : a) : (b > c ? b : a > c ? c : a));
}

// 获取最大值

public static int getMaxNum(int a, int b, int c)

 {

return (a < b ? (b < c ? c : b) : (a < c ? c : a));
}
// 获取最小值

public static int getMinNum(int a, int b, int c) 

{

return (a > b ? (b > c ? c : b) : (a > c ? c : a));

}

例子:  编写主函数,提示用户通过键盘输入一个3位整数,判断是否为有效输入,调用一个子函数,取出该整数的各个位数并重新排列,输出可能的最大整数和最小整数。

// 获取最大数字
int const& getMaxNum(int const& x, int const& y, int const& z);

// 获取最小数字
int const& getMinNum(int const& x, int const& y, int const& z);

// 获取中间数字
int const& getMidNum(int const& x, int const& y, int const& z);

// 数字的新组合
void getNewCombination(int const& num);
// 数字的新组合
void getNewCombination(int const& num)
{

int hBit;// 百位

int tBit;// 十位

int oBit;// 个位


int max = 0,min = 0;

int t = 0;


hBit = num / 100;

tBit = (num/10)%10;

oBit = ((num%100)%10);


max = getMaxNum(hBit, tBit, oBit);

min = getMinNum(hBit, tBit, oBit);

t = getMidNum(hBit,tBit,oBit);


cout<<"The Maximum num is :  " << (max*100 + t*10 + min)<<endl;

cout<<"The Minimum num is :  " << (min*100 + t*10 + max)<<endl;

}
// 获得最大的数字
int const& getMaxNum(int const& x, int const& y, int const& z)
{

int temp = (x>y)? x : y;

return temp > z ? temp : z;

}
// 求中间大小的数字
int const& getMidNum(int const& x, int const& y, int const& z)
{

return (x < y ? (y < z ? y : x < z ? z : x) : (y > z ? y : x > z ? z : x));  

}
// 获取最小的数
int const& getMinNum(int const& x, int const& y, int const& z)
{

int temp = (x<y)? x : y;

return temp<z ? temp : z;

}
void main()
{

int num;


do 

{

cout<<"请输入一个三位数 :  ";

cin>>num;

if (num < 100 || num > 1000)

{

MessageBox(NULL,"数值范围为100--999","提示",MB_OK);

}

} while (num < 100 || num > 1000);


getNewCombination(num);

cout<<endl;

}

【本文转至】:点击打开链接

http://blog.csdn.net/bboyfeiyu/article/details/7382821

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值