zoukankan      html  css  js  c++  java
  • 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    【本文链接】

    http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html

    题目】

    写一个函数,求两个整数的之和,要求在函数体内不得使用+、-、×、÷。

    分析

    这是一道考察发散思维的很有意思的题目。当我们习以为常的东西被限制使用的时候,如何突破常规去思考,就是解决这个问题的关键所在。

    普通四则运算都不能用,那还能用什么啊?我们会想到位运算,因为四则运算本质上都可以通过位运算实现。

    举个例子:

    首先我们可以分析如何做十进制的加法的,比如是如何得出5+17=22这个结果的。实际上,我们可以分成三步的:

    (1)只做各位相加不进位,此时相加的结果是12(个位数5和7相加不要进位是2,十位数0和1相加结果是1);

    (2)做进位,5+7中有进位,进位的值是10;

    (3)第三步把前面两个结果加起来,12+10的结果是22,刚好5+17=22。

    对应的二进制表示:5的二进制是101,17的二进制10001。

    还是试着把计算分成三步:

    (1)各位相加但不计进位,得到的结果是10100(最后一位两个数都是1,相加的结果是二进制的10。这一步不计进位,因此结果仍然是0);

    (2)记下进位,在这个例子中只在最后一位相加时产生一个进位,结果是二进制的10;

    (3)把前两步的结果相加,得到的结果是10110,正好是22。由此可见三步走的策略对二进制也是管用的。

    接下来我们试着把二进制上的加法用位运算来替代。

    (1)第一步不考虑进位,对每一位相加。0加0与 1加1的结果都0,0加1与1加0的结果都是1。我们可以注意到,这和异或的结果是一样的。对异或而言,0和0、1和1异或的结果是0,而0和1、1和0的异或结果是1。

    (2)第二步进位,对0加0、0加1、1加0而言,都不会产生进位,只有1加1时,会向前产生一个进位。此时我们可以想象成是两个数先做位与运算,然后再向左移动一位。只有两个数都是1的时候,位与得到的结果是1,其余都是0。

    (3)第三步把前两个步骤的结果相加。如果我们定义一个函数AddWithoutArithmetic,第三步就相当于输入前两步骤的结果来递归调用自己。

    【代码】

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
     
    // 52_AddWithoutArithmetic.cpp : Defines the entry point for the console application.
    //
    /*
        version: 1.0
        author: hellogiser
        blog: http://www.cnblogs.com/hellogiser
        date: 2014/5/24
    */


    #include "stdafx.h"
    #include <iostream>
    using namespace std;

    // add two numbers without arithmetic recursively
    int AddWithoutArithmetic_Recursively(int a, int b)
    {
        
    if(b == 0)
            
    return a;
        
    int sum = a ^ b;
        
    int carry = (a & b) << 1;
        
    return AddWithoutArithmetic_Recursively(sum, carry);
    }

    // compact version
    int add(int a, int b)
    {
        
    return b == 0 ? a : add(a ^ b, (a & b) << 1);
    }

    // add two numbers without arithmetic iteratively
    int AddWithoutArithmetic_Iteratively(int a, int b)
    {
        
    int sum, carry;
        
    do
        {
            sum = a ^ b;
            carry = (a & b) << 
    1;

            a = sum;
            b = carry;
        }
        
    while (carry != 0);
        
    return sum;
    }

    void test_base(int a, int b)
    {
        cout << AddWithoutArithmetic_Recursively(a, b) << endl;
        cout << AddWithoutArithmetic_Iteratively(a, b) << endl;
        cout << 
    "----------------------------------------- ";
    }

    void test_case()
    {
        test_base(
    57); // 12
        test_base(5, -7); // -2
        test_base(-5, -7); // -12
    }

    int _tmain(int argc, _TCHAR *argv[])
    {
        test_case();
        
    return 0;
    }

    扩展】

    不用新的变量,交换2个变量的值。

    (1)基于加减

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    // by hellogiser
    // swap without additional variables
    // use add/sub
    void swap(int &a, int &b)
    {
        a = a + b;
        b = a - b;
        a = a - b;
    }

    (2)基于异或

     C++ Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    // by hellogiser
    // swap without additional variables
    // use xor
    void swap(int &a, int &b)
    {
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
    }

    【参考】

    http://www.cnblogs.com/MrGreen/p/3491570.html

    http://zhedahht.blog.163.com/blog/static/254111742011125100605/

    【本文链接】

    http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html

    个人学习笔记,欢迎拍砖!---by hellogiser

    Author: hellogiser
    Warning: 本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,且在文章页面明显位置给出原文连接。Thanks!
    Me: 如果觉得本文对你有帮助的话,那么【推荐】给大家吧,希望今后能够为大家带来更好的技术文章!敬请【关注】
  • 相关阅读:
    超强视频分割/剪辑软件-Ultra Video Splitter绿色便携版
    超强视频分割/剪辑软件-Ultra Video Splitter绿色便携版
    ASP.NET做WEB开发的工具选择
    ASP.NET做WEB开发的工具选择
    C#中的局部类型
    OpenCV2:幼儿园篇 第四章 访问图像
    OpenCV2:幼儿园篇 第三章 导出图像
    OpenCV2:幼儿园篇 第二章 读取图像
    OpenCV2:幼儿园篇 第一章 创建图像并显示
    MFC隐藏在黑暗之中的大坑
  • 原文地址:https://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html
Copyright © 2011-2022 走看看