程序员面试金典——18.1另类加法
Solution1:还是参考剑指上的思路。。
class UnusualAdd {
public:
int addAB(int A, int B) {
// write code here
int sum = 0, carry = 0;
do {
sum = A^B;
carry = (A & B) << 1;
A = sum;
B = carry;
}while(B != 0);
return A;
}
};
另类加法算法解析
本文介绍了一种不使用常规加法运算实现两整数相加的方法。通过位操作实现加法逻辑,避免了直接使用加号进行计算,提供了一个具体的C++实现案例。
268

被折叠的 条评论
为什么被折叠?



