【每天一道编程系列-2018.3.11】—— A + B Problem
2018-03-11 23:36
447 查看
【题目描述】
Write a function that add two numbers A and B. You should not use
Are a and b both
Can I use bit operation?Sure you can.
【题目大意】
给出两个整数a和b, 求他们的和, 但不能使用
注意事项你不需要从输入流读入数据,只需要根据
我可以使用位运算符么?当然可以
【本题答案】
package blog;
/**
* @author yesr
* @create 2018-03-11 下午11:28
* @desc
**/
public class Test0310 {
/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
private static int plus(int a, int b) {
if (b == 0)
return a;
else
return plus(a^b, (a&b) << 1);
}
public static void main(S
a0e7
tring[] args) {
System.out.println(plus(3,0));
}
}
Write a function that add two numbers A and B. You should not use
+or any arithmetic operators. NoticeThere is no need to read data from standard input stream. Both parameters are given in function
aplusb, you job is to calculate the sum and return it.Clarification
Are a and b both
32-bitintegers?Yes.
Can I use bit operation?Sure you can.
【题目大意】
给出两个整数a和b, 求他们的和, 但不能使用
+等数学运算符。
注意事项你不需要从输入流读入数据,只需要根据
aplusb的两个参数a和b,计算他们的和并返回就行。说明a和b都是
32位整数么?是的
我可以使用位运算符么?当然可以
【本题答案】
package blog;
/**
* @author yesr
* @create 2018-03-11 下午11:28
* @desc
**/
public class Test0310 {
/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
private static int plus(int a, int b) {
if (b == 0)
return a;
else
return plus(a^b, (a&b) << 1);
}
public static void main(S
a0e7
tring[] args) {
System.out.println(plus(3,0));
}
}
相关文章推荐
- 【每天一道编程系列-2018.3.2】(Ans)
- 【每天一道编程系列-2018.3.6】(Ans)
- 【每天一道编程系列-2018.1.26】(Ans)
- 【每天一道编程系列-2018.1.29】(Ans)
- 【每天一道编程系列-2018.2.10】(Ans)
- 【每天一道编程系列-2018.3.7】(Ans)
- 【每天一道编程系列-2018.1.31】(Ans)
- 【每天一道编程系列-2018.2.1】(Ans)
- 【每天一道编程系列-2018.3.19】—— Digit Counts
- 【每天一道编程系列-2018.2.13】(Ans)
- 【每天一道编程系列-2018.1.26】
- 【每天一道编程系列-2018.2.5】(Ans)
- 【每天一道编程系列-2018.2.22】(Ans)
- 【每天一道编程系列-2018.2.6】(Ans)
- 【每天一道编程系列-2018.2.21】(Ans)
- 【每天一道编程系列-2018.2.18】(Ans)
- 【每天一道编程系列-2018.3.1】(Ans)
- 【每天一道编程系列-2018.2.11】(Ans)[补]
- 【每天一道编程系列-2018.2.27】(Ans)
- 【每天一道编程系列-2018.3.5】(Ans)