1.A+B问题 给出两个整数A和B, 求他们的和, 但不能使用 + 等数学运算符。

class Solution {
public:
    /*
     * @param : An integer
     * @param : An integer
     * @return: The sum of a and b
     */

    int aplusb(int a, int b) 

  {

        // write your code here
    if(b==0) //递归结束条件:如果右加数为0,即不再有进位了,则结束。  
        return a;  
    int s = a^b;    //加和
    int c = (a&b)<<1; //进位左移1位,达到进位的目的。  
        cout<<s  <<c  <<endl;


    return aplusb(s, c); //再把'和'和'进位'相加。递归实现。 
                                   
    }
};

猜你喜欢

转载自blog.csdn.net/zichen7055/article/details/78427226
今日推荐