三个数求最大数、最小数、中间数
2012-03-22 13:45
169 查看
// 获取中间值
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;
}
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;
}
相关文章推荐
- 三个数求最大数、最小数、中间数
- 【C++基本功补习】查找三个数据中的最大值、最小值、中间值,两种实现方案比较
- 华为2014年机试题1 (输入1--50个数字,求出最小数和最大数的和 )
- java求三个整数的最大值
- 获取一个整型数组前三个最大的值
- java int a,b,c 三个数判断输出最大的一个
- Python实现:指定三个数相加之和, 使这个三个数乘积最大
- 比较三个整数大小,输出最大值
- 输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。写三个函数; ①输入10个数;②进行处理;③输出10个数
- 三个数求最大值
- 图像处理-找出三个苹果中最大的一个
- 求三个正整数中的最大数
- 三个div中间的可以缩放
- 输入3个数,求最大值最小值中间值
- 第五周项目二(2)比较三个数,输出最大值
- 将一个5*5 矩阵中最大的放在中间,四个角分别放最小的数据,顺序分别是左右上下,通过函数完成。
- java取出三个日期中的最大日期
- js 小数自动补齐两位 并判断最大值
- js 小数自动补齐两位 并判断最大值
- 输入三个整数,输出最大数和最小数