两个变量a,b,不用“if”,“? :”,switch或者其它判断语句,找出两个数中间比较大的
2013-08-20 16:02
591 查看
问题
有两个变量a,b,不用“if”,“? :”,switch或者其它判断语句,找出两个数中间比较大的。
结果为:
有两个变量a,b,不用“if”,“? :”,switch或者其它判断语句,找出两个数中间比较大的。
解决方案
//---------------------------------------------------
// 环境:VS2005
// 用途:比较两个数大小测试
// 时间:2010.9.25
// 作者:http://pppboy.blog.163.com
//---------------------------------------------------
#include "stdafx.h"
#include <iostream>
using namespace std;
/*
方法1:取平均值法
大的为 ((a+b)+abs(a-b)) / 2
小的为 (a+b - abs(a-b)) / 2
*/
int fMax1(int a, int b)
{
return ((a+b)+abs(a-b)) / 2;
}
/*
方法2:不使用abs()
a<b时,a/b=0,所以前面为b*(b/a),后面为b/a,那么结果就是b
a=b时,a/b=1,所以前面为a+b=2a,后面为2,那么结果就是a
a>b时,b/a=0,所以前面为a*(a/b),后面为a/b,那么结果就是a
*/
int fMax2(int a, int b)
{
int larger = (a*(a/b) + b*(b/a))/(a/b + b/a);
//long smaller = (b*(a/b) + a*(b/a))/(a/b + b/a);
return larger;
}
/*
方法3:如果取 a/b 余数不为0,则说明a>b
这是个好方法,不过题目说了,不能用“? :”
*/
int fMax3(int a, int b)
{
return (a / b) ? a : b;
}
/*
方法4:移位法
当b<0的时候以补码存,故最高位是1
所以右移31位b>>31其实就是最高位的值
b>=0时候最高位为0
所以b跟1与时候为b,a=a-(a-b)=b
b跟1作与运算时候为0,相当于a=a-0=a
*/
int fMax4(int a, int b)
{
b = a - b;
a -= b & (b>>31);
return a;
}
//移位法
int fMax5(int a,int b)
{
int c[2] = {a, b};
int z = a - b;
z = (z>>31)&1;
return c[z];
}
//移位法
int fMax6(int a, int b)
{
int flag = ((a - b) >> 31)&1;
return a - (a - b) * flag;
}
//我想这个应该是最牛B的一个
int fMax7(int a, int b)
{
int pair[2] = {a, b};
return pair[a < b];
}
int main(int argc, char* argv[])
{
int a, b;
cout << "-------------------------------------------------" << endl;
cout << "input a :" << endl;
cin >> a;
cout << "input b :" << endl;
cin >> b;
cout << "-------------------------------------------------" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "-------------------------------------------------" << endl;
cout << "(fMax1)the max number is : " << fMax1(a, b) << endl;
cout << "(fMax2)the max number is : " << fMax2(a, b) << endl;
cout << "(fMax3)the max number is : " << fMax3(a, b) << endl;
cout << "(fMax4)the max number is : " << fMax4(a, b) << endl;
cout << "(fMax5)the max number is : " << fMax5(a, b) << endl;
cout << "(fMax6)the max number is : " << fMax6(a, b) << endl;
cout << "-------------------------------------------------" << endl;
system("pause");
return 0;
}
结果为:
-------------------------------------------------
input a :
54
input b :
78
-------------------------------------------------
a = 54
b = 78
-------------------------------------------------
(fMax1)the max number is : 78
(fMax2)the max number is : 78
(fMax3)the max number is : 78
(fMax4)the max number is : 78
(fMax5)the max number is : 78
(fMax6)the max number is : 78
-------------------------------------------------
请按任意键继续. . .
相关文章推荐
- 有两个变量a 和 b,不用if、?: 、switch 或者其他判断语句,找出两个数中比较大的那个
- 有两个变量a 和 b,不用if、?: 、switch 或者其他判断语句,找出两个数中比较大的那个
- 有两个变量a和b,不用“if”、“? :”、“switch”或其他判断语句,找出两个数中比较大的
- 不使用if、?:、switch及其他判断语句如何找出两个int型变量中的最大值和最小值
- 两个变量a,b,不用判断语句,找出两个中比较大的那个变量
- 两个变量a,b,不用判断语句,找出两个中比较大的那个变量
- 【面试题】不用if,条件表达式,switch等判断语句------找出两个数中较大的
- 找出两个int型变量的最大值和最小值,不使用if/:?/switch判断语句
- 不用if,.?,switch等判断语句,求两变量的大小
- 关于不用if,switch,?:语句判断两个数大小及其他
- 不用那if,switch,?:语句判断两个数的大小
- 不用”if“,”?:“,”switch“或其他判断语句,求两个数中较大的数或较小的数
- 比较两个数a、b的大小,不能使用大于/小于、if、switch,?:等判断语句
- 关于不用if,switch,?:语句判断两个数大小及其他
- 有两个变量a和b,不用if等判断语句,求两者较大的值
- 比较两个数a、b的大小,不能使用大于/小于、if、switch,?:等判断语句
- 选出两个数中较大者,不用if, ? : ,switch等判断语句.
- 不用任何比较判断找出两个数中较大的数
- 不用任何比较判断找出两个数中较大的数
- 定义一个宏,比较两个数a、b的大小,不能使用大于、小于、if语句 以及 不用 第三个数,交换 a,b的值