三个数求最大数、最小数、中间数
2014-10-18 18:32
232 查看
// 获取中间值
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;
}
【本文转至】:点击打开链接
/article/1516381.html
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;
}
【本文转至】:点击打开链接
/article/1516381.html
相关文章推荐
- 三个数求最大数、最小数、中间数
- 【C++基本功补习】查找三个数据中的最大值、最小值、中间值,两种实现方案比较
- 8.7 输入10个整数,用函数编程将其中最大数与最小数位置互换,然后输出互换后的数组
- 求三个数中最大的数
- 求出数组中相邻三个数之和最大的~
- 有三个整数a b c,由键盘输入,输出其中的最大的数。
- codevs 1201 最小数和最大数
- 输入10个整数,将其中最小的数与第一个数对换,把最大的数和最后一个数对换,写三个函数:1.输入10个数,2.进行处理.3输出10个数
- 三个角度看SVM(1)——最大间隔分类器
- codevs 1201 最小数和最大数
- JAVA学习笔记_三个数判断最大数
- 给定三个整数, 找出中间那个数, 要求比较次数尽量少,即使是最坏情况
- 任意给出一个四位数, 把它重新组成一个四位的最大数和一个最小数, 算出两者间的差.
- 【C】辗转相除法求两个数的最大公约数,利用位运算交换两个数无须中间变量
- 求三个数中最大数和最小数的差值
- 大数减小数求最大公约数
- 键盘输入三个数,输出其中最大的数
- 三个数字的中间值
- hunnu10701(在n个点中选三个点,使得构成的面积最大)
- 输入三个整数。输出最大最小值