zoukankan      html  css  js  c++  java
  • 不用判断语句如if,?:等来实现比较2个数

    、说明

    前两天笔试的时候居然出现这么个题目,其它的那些题目都答的比较好,就这一个不会做。

    二、问题

    有两个变量a,b,不用“if”,“? :”,switch或者其它判断语句,找出两个数中间比较大的。

    三、解决方案

    在网上找到了好多方案。

    //--------------------------------------------------- 
    // 环境:VS2010 
    // 用途:比较两个数大小测试 
    // 时间: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 
    ------------------------------------------------- 
    请按任意键继续. . .

    四、引用申明

    这几个解决方法都是从网上找到的,这里给出作者的链接,谢谢作者,这里仅供本人学习。

    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

  • 相关阅读:
    【java】JDBC连接MySQL
    【java】网络socket编程简单示例
    【java】对象序列化Serializable、transient
    【java】扫描流Scanner接收输入示例
    【java】缓冲字符字节输入输出流:java.io.BufferedReader、java.io.BufferedWriter、java.io.BufferedInputStream、java.io.BufferedOutputStream
    【java】System成员输入输出功能out、in、err
    33-Java中的String,StringBuilder,StringBuffer三者的区别
    5-Error:failed to find Build Tools revision 28.0.0 rc1解决方案
    46-wxpython 4 使用 grid 展示表格
    45-暴力密码字典
  • 原文地址:https://www.cnblogs.com/itdreamfly/p/12871717.html
Copyright © 2011-2022 走看看