LintCode-A + B 用位操作模拟加法
- class Solution {
- public:
- /*
- * @param a: The first integer
- * @param b: The second integer
- * @return: The sum of a and b
- */
- int aplusb(int a, int b) {
- // write your code here, try to do it without arithmetic operators.
- if(a==)return b;
- if(b==)return a;
- int x1 = a^b;
- int x2 = (a&b)<<;
- return aplusb(x1,x2);
- }
- };
俩位相加有四种情况:
1. 1 + 1 = 10;
2. 1 + 0 = 01;
3. 0 + 1 = 01;
4. 0 + 0 = 00;
第一种情况,即有进位,用 (a&b)<<1解决;
第二第三种情况,用a^b解决。
LintCode-A + B 用位操作模拟加法的更多相关文章
- qq教xixi写模拟加法【非常爆炸】
#include<iostream> #include<cstdio> #include<math.h> #include<queue> #includ ...
- 为什么位运算可以实现加法(1、 不考虑进位的情况下位运算符中的异或^可以表示+号)(2、 位运算符中的与运算符&和左移运算符<<可以模拟加法中的进位)(3、位运算不仅可以做加法,还可以做其它的乘法减法等:计算机本质是二进制运算)
为什么位运算可以实现加法(1. 不考虑进位的情况下位运算符中的异或^可以表示+号)(2. 位运算符中的与运算符&和左移运算符<<可以模拟加法中的进位)(3.位运算不仅可以做加法,还 ...
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)
题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...
- 【LintCode·容易】用栈模拟汉诺塔问题
用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...
- 基本类型算法题目学习(EPI)
1.关于奇偶校验的方法中,如何快速的求取一个64-bit的数字的奇偶校验位.(如果1的位数为奇数,则奇偶校验位为1,如果1的位数为偶数,则奇偶校验位为0) a.暴力枚举法采用一位一位进行计算,一位一位 ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- [leetcode] 位操作题解
子集 题目[78]:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [ ...
- hdu1753()模拟大型实景数字相加
标题信息: 手动求大的实数在一起, pid=1753">http://acm.hdu.edu.cn/showproblem.php?pid=1753 AC代码: /** *大实数相加 ...
- Codeforces Round #550 (Div. 3) E. Median String (模拟)
Median String time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- Deploy a Sharded Cluster
Start the Config Server Database Instances for example : mongod --configsvr --dbpath <path> - ...
- HDU 5418 Victor and World (Floyd + 状态压缩DP)
题目大意:从起点 1 开始走遍所有的点,回到起点 1 ,求出所走的最短长度. 思路:首先利用 Floyed 求出任意两点之间的最短距离 dis[i][j].求出任意两点之间的最短距离后,运用动态规划. ...
- <转载>Wait and Waitpid
转载http://www.cnblogs.com/lihaosky/articles/1673341.html 一.Wait #include <sys/types.h> /* 提供类型p ...
- js jquery 验证写法
<?php header("Content-type: text/html; charset=utf-8"); ?> <script src="jque ...
- python list 按长度分段
def changes(a,b): #list 分段函数,a:数据[(1),(2)]b:长度 for i in xrange(0,len(a),b): yield a[i:i+b] for i in ...
- PHP操作MySQL数据库的相关函数
首先,要分清SQL语句的类型: SQL语句的分类 (1)DDL:Data Define Language,数据定义语言--定义表的列结构 CREATE.DROP.ALTER.TRUNCATE (2)D ...
- 2014 BDTC 參会有感
中国大数据技术大会(Big Data Technology Conference,BDTC)是眼下国内最具影响.规模最大的大数据领域的技术盛会. 大会的前身是Hadoop中国云计算大会(Hadoop ...
- mybatis参数查询
单个参数查询 在mapper.xml配置文件中配置 <select id= "selectByNu" paramet ...
- jQuery validate 的valid()方法一直返回true
1 调用$('#myForm').valid(),一直返回ture eg:html <form id="myForm"> <input class="f ...
- .net对象转Datable
public static DataTable GetDataTable<T>( IEnumerable<T> list,string tableName) { DataTab ...