LintCode: A + B Problem

简介:

C++

复制代码
 1 class Solution {
 2 public:
 3     /*
 4      * @param a: The first integer
 5      * @param b: The second integer
 6      * @return: The sum of a and b
 7      */
 8     int aplusb(int a, int b) {
 9         // write your code here, try to do it without arithmetic operators.
10         int sum = a^b;
11         int carry = (a&b) << 1;
12         int tmp;
13         while (carry) {
14             tmp = sum;
15             sum = tmp^carry;
16             carry = (tmp&carry) << 1;
17         }
18         return sum;
19     }
20 };
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/4998708.html,如需转载请自行联系原作者

相关文章
HDOJ1002题A + B Problem II,2个大数相加
HDOJ1002题A + B Problem II,2个大数相加
132 0
|
机器学习/深度学习 C++
【HDU 5572 An Easy Physics Problem】计算几何基础
2015上海区域赛现场赛第5题。 题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5572 题意:在平面上,已知圆(O, R),点B、A(均在圆外),向量V。
1048 0
|
Java 文件存储
HDOJ(HDU) 2132 An easy problem
HDOJ(HDU) 2132 An easy problem
129 0

热门文章

最新文章