A + B 问题
给出两个整数a和b, 求他们的和, 但不能使用 +
等数学运算符。
直接上源码
C++解法:
public class Solution {
/*
* @param a: An integer
* @param b: An integer
* @return: The sum of a and b
*/
public int aplusb(int a, int b) {
// write your code here, try to do it without arithmetic operators.
if(a==0) return b; //不再进位
if(b==0) return a; //不进位操作为零,直接取进位操作的结果
int sum,i;
i=a^b; //不进位相加的结果
sum=(a&b)<<1; //进位的结果
return aplusb(sum,i); // 两者再进行相加
}
}
Python解法:
class Solution:
# """
# @param a: An integer
# @param b: An integer
# @return: The sum of a and b
# """
def aplusb(self, a, b):
# write your code here
if(a==0) :
return (b)
if(b==0) :
return (a)
i=a^b
sum = (a&b)<<1
return self.aplusb(i, sum)