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的更多相关文章

  1. [Algorithm] Universal Value Tree Problem

    A unival tree (which stands for "universal value") is a tree where all nodes under it have ...

  2. 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 ...

  3. 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 ...

  4. 简单的量子算法(一):Hadamard 变换、Parity Problem

    Hadamard Transform Hadamard 变换在量子逻辑门中提过,只不过那时是单量子的Hadamard门,负责把\(|1\rangle\)变成\(|-\rangle\),\(|0\ran ...

  5. 《Paxos Made Simple》翻译

    1 Introduction 可能是因为之前的描述对大多数读者来说太过Greek了,Paxos作为一种实现容错的分布式系统的算法被认为是难以理解的.但事实上,它可能是最简单,最显而易见的分布式算法了. ...

  6. POMDP

    本文转自:http://www.pomdp.org/ 一.Background on POMDPs We assume that the reader is familiar with the val ...

  7. Design and Analysis of Algorithms_Decrease-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  8. Design and Analysis of Algorithms_Divide-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  9. 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 ...

随机推荐

  1. java笔记之IO1

    File:文件和目录(文件夹)路径名的抽象表示形式 * 构造方法: *   File(String pathname):根据一个路径得到File对象 *   File(String parent, S ...

  2. 【黑金教程笔记之004】【建模篇】【Lab 03 消抖模块之一】—笔记

    设计思路: (1)       一旦检测到按键资源按下(从高电平到低电平),“电平检测模块”就会拉高H2L_Sig电平,然后拉低. (2)       “10ms延迟模块”检测到H2L_Sig高电平, ...

  3. 洛谷 P2764 最小路径覆盖问题【匈牙利算法】

    经典二分图匹配问题.把每个点拆成两个,对于原图中的每一条边(i,j)连接(i,j+n),最小路径覆盖就是点数n-二分图最大匹配.方案直接顺着匹配dsf.. #include<iostream&g ...

  4. oracle ORA-01704: string literal too long问题分析

    今天使用sql在oracle直接insert update一个表时,出现ORA-01704: string literal too long的错误,我们的sql是 update mall_config ...

  5. 史上最详细最全的Linux上安装Oracle的教程-centos7

    一.安装Oracle前准备 1.创建运行oracle数据库的系统用户和用户组 [humf@localhost ~]$ su root #切换到root Password: [root@localhos ...

  6. 洛谷 P3368 【模板】树状数组 2(区间修改点查询)

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的值 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  7. 【洛谷3343_BZOJ3925】[ZJOI2015]地震后的幻想乡(状压 DP_期望)

    题目: 洛谷 3343 BZOJ 3925 分析: 谁给我说这是个期望概率神题的,明明没太大关系好吧 「提示」里那个结论哪天想起来再问 Jumpmelon 怎么证. 首先,由于开始修路前 \(e_i\ ...

  8. AsyncTask官方教程-推荐用AsyncTask少用Thread

    Using AsyncTask AsyncTask allows you to perform asynchronous work on your user interface. It perform ...

  9. 转 【TTS】AIX平台数据库迁移到Linux--基于RMAN(真实环境)

    [TTS]AIX平台数据库迁移到Linux--基于RMAN(真实环境) http://www.cnblogs.com/lhrbest/articles/5186933.html 各位技术爱好者,看完本 ...

  10. poj2886 Who Gets the Most Candies?

    思路: 先打反素数表,即可确定因子最多的那个数.然后模拟踢人的过程确定对应的人名.模拟的过程使用线段树优化加速. 实现: #include <cstdio> #include <cs ...