[C++]不用if比较两个数大小
2014-04-29 17:00
519 查看
2010-09-25 18:31:13| 分类: [C++] | 标签:c++ 不用if 比较大小 |举报|字号 订阅
结果为:
http://blog.csdn.net/itegel84/archive/2008/04/02/2245680.aspx
http://blog.csdn.net/bbjjll0331/archive/2010/06/05/5650127.aspx
http://www.chinaunix.net/jh/23/422544.html
http://www.devmaster.net/forums/showthread.php?t=428
http://hi.baidu.com/onlyalone/blog/item/65852fa4563ff5f19052ee96.html
一、说明
前两天笔试的时候居然出现这么个题目,其它的那些题目都答的比较好,就这一个不会做。二、问题
有两个变量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 。负数补码符号位是1,正数补码符号位是0
所以右移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
-------------------------------------------------
请按任意键继续. . .
四、引用申明
这几个解决方法都是从网上找到的,这里给出作者的链接,谢谢作者,这里仅供本人学习。http://blog.csdn.net/itegel84/archive/2008/04/02/2245680.aspx
http://blog.csdn.net/bbjjll0331/archive/2010/06/05/5650127.aspx
http://www.chinaunix.net/jh/23/422544.html
http://www.devmaster.net/forums/showthread.php?t=428
http://hi.baidu.com/onlyalone/blog/item/65852fa4563ff5f19052ee96.html
相关文章推荐
- C++ 不用判断运算符进行比较两个数的大小
- 不用if比较两个数大小
- 不用if比较两个数大小
- 一个宏比较两个数的大小(不用>,if语句)
- 一个宏比较两个数的大小(不用>,if语句)
- 不用大于、小于、if语句比较两个数的大小
- 比较两个数的大小--不用大于小于if
- 程序员面试宝典p40----不用if比较两个数大小
- 不用if比较两个数大小
- 请定义一个宏,比较两个数的a、b的大小,不能使用大于、小于、if语句
- 不用判断语句比较两个数的大小值
- 不用比较运算符比较两个数的大小
- 请定义一个宏,比较两个数的a、b的大小,不能使用大于、小于、if语句
- 08-14 比较两个数的大小,不用判断语句
- 请定义一个宏,比较两个数的a、b的大小,不能使用大于、小于、if语句
- 关于不用if,switch,?:语句判断两个数大小及其他
- 不用比较运算符比较两个数的大小
- 不用比较操作符和 if / switch / ?: 比较两数大小,并求最大值
- 比较两个数的大小,要求不能用if,<,>,?
- 不用IF比较两数大小