A + B 问题

给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符。

样例

如果 a=1 并且 b=2,返回3

注意

你不需要从输入流读入数据,只需要根据aplusb的两个参数a和b,计算他们的和并返回就行。

挑战

显然你可以直接 return a + b,但是你是否可以挑战一下不这样做?

 

 

 1 class Solution {
 2     /*
 3      * param a: The first integer
 4      * param b: The second integer
 5      * return: The sum of a and b
 6      */
 7     public int aplusb(int a, int b) {
 8         int c = (a&b)<<1;
 9         int x = a^b;
10         if(c == 0) return x;
11         else return aplusb(c,x);
12     }
13 };
View Code

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-07
  • 2021-08-27
  • 2021-09-05
猜你喜欢
  • 2021-05-25
  • 2021-09-01
  • 2022-12-23
  • 2021-10-03
  • 2022-02-08
  • 2022-12-23
相关资源
相似解决方案