[Algorithm] 1. A+B Problem
Description
Write a function that add two numbers A and B.
Clarification
Are a and b both 32-bit
integers?
- Yes.
Can I use bit operation?
- Sure you can.
Example
Given a=1
and b=2
return 3
.
Challenge
Of course you can just return a + b to get accepted. But Can you challenge not do it like that?(You should not use +
or any arithmetic operators.)
My Answer
Using a recursion method to solve this problem!
/**
* @param a: An integer
* @param b: An integer
* @return: The sum of a and b
*/
int aplusb(int a, int b) {
// Recursion process
if ( (a & b) == ){
return a ^ b;
} else {
return aplusb( (a^b), ((a&b)<<) );
}
}
Tips
It's not the only way to get the right answer. Can you try the other way like the loop structure?
[Algorithm] 1. A+B Problem的更多相关文章
- [Algorithm] Universal Value Tree Problem
A unival tree (which stands for "universal value") is a tree where all nodes under it have ...
- Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...
- 简单的量子算法(一):Hadamard 变换、Parity Problem
Hadamard Transform Hadamard 变换在量子逻辑门中提过,只不过那时是单量子的Hadamard门,负责把\(|1\rangle\)变成\(|-\rangle\),\(|0\ran ...
- 《Paxos Made Simple》翻译
1 Introduction 可能是因为之前的描述对大多数读者来说太过Greek了,Paxos作为一种实现容错的分布式系统的算法被认为是难以理解的.但事实上,它可能是最简单,最显而易见的分布式算法了. ...
- POMDP
本文转自:http://www.pomdp.org/ 一.Background on POMDPs We assume that the reader is familiar with the val ...
- Design and Analysis of Algorithms_Decrease-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Design and Analysis of Algorithms_Divide-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Examples of complexity pattern
O(1):constant - the operation doesn't depend on the size of its input, e.g. adding a node to the tai ...
随机推荐
- word excel 等导出相关操作
无插件,无com组件,利用EXCEL.WORD模板做数据导出(一) http://www.cnblogs.com/tzy080112/p/3413938.html 使用Aspose.Cells组件生成 ...
- jquery的validate的用法
//引入js文件 //jquery 文件 <script src="__PUBLIC__/static/wap/js/jquery.min.js?v=2.1.4">&l ...
- java 继承还是组合
1.我真的需要上溯转型(upcasting)吗? 如果是,才会用到继承. 2.
- [POI2001]Goldmine
Description Byteman作为Byteland的The Goldmine(某一公司厂矿)的最有功的雇员之一,即将在年末退休.为了表示对他的 认真勤恳的工作的承认,The Goldmine的 ...
- poj 1511 Invitation Cards spfa 邻接矩阵
题目链接: http://poj.org/problem?id=1511 题目大意: 这道题目比较难理解,我读了好长时间,最后还是在队友的帮助下理解了题意,大意就是,以一为起点,求从一到其他各点的最短 ...
- 人工智能-深度学习(3)TensorFlow 实战一:手写图片识别
http://gitbook.cn/gitchat/column/59f7e38160c9361563ebea95/topic/59f7e86d60c9361563ebeee5 wiki.jikexu ...
- 递推DP UVA 607 Scheduling Lectures
题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...
- 数论(GCD) HDOJ 4320 Arcane Numbers 1
题目传送门 题意:有一个A进制的有限小数,问能否转换成B进制的有限小数 分析:0.123在A进制下表示成:1/A + 2/(A^2) + 3 / (A^3),转换成B进制就是不断的乘B直到为0,即(1 ...
- 题解报告:poj 1195 Mobile phones(二维BIT裸题)
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- 牛客小白月赛5-I-区间(差分求前缀和+一次暴力统计)
题目描述 Apojacsleam喜欢数组. 他现在有一个n个元素的数组a,而他要对a[L]-a[R]进行M次操作: 操作一:将a[L]-a[R]内的元素都加上P 操作二:将a[L]-a[R]内的元素都 ...