[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 ...
随机推荐
- Java位运算(移位,位与,或,异或,非)
1.左移( << ) // 0000 0000 0000 0000 0000 0000 0000 0101 然后左移2位后,低位补0:// // 0000 0000 0000 0000 0 ...
- bzoj 4720: [Noip2016]换教室【期望dp】
状压dp,设f[i][j][0/1]为前i个时间段换了j间教室的期望体力消耗,转移很好想(但是写起来好长= =) #include<iostream> #include<cstdio ...
- P3171 [CQOI2015]网络吞吐量
传送门 首先跑一遍最短路,如果一条边满足\(dis[v]=dis[u]+w[i]\),那么这条边就在最短路中,把它加进网络流的图里 然后点的流量限制的话拆点,把每个点拆成两个,中间连边来限制流量 最后 ...
- linux自动删除30天前的日志文件
linux应用总结: 自动删除n天前的日志文件: . 使用的命令格式如下: find 对应目录 -mtime +天数 -name "文件名" -exec -rm -rf -name ...
- urllib的高级用法
Handler简介 我们可以把他理解为各种处理器,有专门处理登录验证的,有处理cookies的,有处理代理设置的.利用他们,我们几乎可以做到HTTP请求中的所有事情. 首先,介绍一下 urllib.r ...
- 循环队列 分类: c/c++ 2014-10-10 23:28 605人阅读 评论(0) 收藏
利用线性表实现队列,为了有效利用空间,将其设计为循环结构,防止假溢出:牺牲一个存储单元以区分队空.队满. 设front队头,rear队尾,N为顺序表大小 队空:rear==front 队满:(rear ...
- Object C学习笔记18-SEL,@ selector,Class,@class--转
一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体介绍参数: - (id)performSelector:(SEL)aSelector; - (id)performSele ...
- Python multiprocessing相关疑问
1. multiprocessing 和 threading有什么区别? threading module并没有真正利用多核.而multiprocessing 利用subprocess避开了pytho ...
- 451 Sort Characters By Frequency 根据字符出现频率排序
给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...
- UWP Windows10开发更新磁贴和动态更新磁贴
下面将介绍两种方式如何在windows10 uwp开发中如何更新应用磁贴: 实际上windows的磁贴就是用xml实现的,你只需要创建相应格式的xml就可以实现动态磁贴了 一,手动更新磁贴 二,轮询更 ...