18.1---不用加号的加法(CC150)
1,自己写的又长又臭的代码,也能AC,但是太丑了。主要是通过二进制来算。
public static int addAB(int a, int b){
int res = 0;
String str1 = Integer.toBinaryString(a);
String str2 = Integer.toBinaryString(b);
ArrayList<Integer> list = new ArrayList();
int digit = 0;
int cur = 0;
int i = str1.length()-1;
int j = str2.length()-1;
while(i >= 0 && j >= 0){
int tmp = 0;
if(str1.charAt(i) == '1'){
System.out.println("here");
if(str2.charAt(j) == '1'){
if(cur == 1){
tmp = 1;
digit = 1;
}
else{
tmp = 0;
digit = 1;
}
}
else{
if(cur == 1){
digit = 1;
tmp = 0;
}
else{
digit = 0;
tmp = 1;
}
}
}
else{
if(str2.charAt(j) == '1'){
if(cur == 1){
digit = 1;
tmp = 0;
}
else{
tmp = 1;
digit = 0;
}
}
else{
if(cur == 1){
digit = 0;
tmp = 1;
}
else{
digit = 0;
tmp = 0;
}
}
}
cur = digit;
digit = 0;
list.add(tmp);
i--;
j--;
}
while(i >= 0){
int tmp = 0;
if(str1.charAt(i) == '1'){
if(cur == 1){
tmp = 0;
digit = 1;
}
else{
tmp = 1;
digit = 0;
}
}
else{
if(cur == 1){
tmp = 1;
digit = 0;
}
else{
digit = 0;
tmp = 0;
}
}
list.add(tmp);
cur = digit;
digit = 0;
i--;
}
while(j >= 0){
int tmp = 0;
if(str2.charAt(j) =='1'){
if(cur == 1){
tmp = 0;
digit = 1;
}
else{
tmp = 1;
digit = 0;
}
}
else{
if(cur == 1){
tmp = 1;
digit = 0;
}
else{
digit = 0;
tmp = 0;
}
}
list.add(tmp);
cur = digit;
digit = 0;
j--;
}
if(cur == 1){
list.add(1);
}
System.out.println(list);
int num = 0;
String str = new String();
for(int k = list.size()-1;k >= 0; k--){
str += list.get(k);
}
System.out.println(str);
return Integer.valueOf(str,2) ;
}
2,CC150课本上的答案。写的非常漂亮,一定要记住了。
思路,1,如果只加不进位,1+1=0,1,0相加1.0+0=0;2,如果看什么时候进位,11的时候。
所以就是a^b,a&b。但进位是往前的,所以,a&b<<1.
答案:
public static int addAB(int a, int b) {
// write code here
if(b == 0) return a;
int sum = a ^ b;//只加不进位
int carry = (a & b) << 1;//因为只有1,1,时候才进位。
return addAB(sum,carry);
}
18.1---不用加号的加法(CC150)的更多相关文章
- python不用加号实现加法
问题: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.E ...
- LeetCode(不用加号的加法)
题目: 设计一个函数把两个数相加,不得使用+或者其他算数运算符. 示例: 输入:a=1,b=1 输出:2 提示: a,b均有可能是负数或0 结果不会溢出32位整数 初始思路: 看到题目我就明白只能用位 ...
- C语言不使用加号实现加法运算的几种方法
今天看到<编码:隐匿在计算机软硬件背后的语言>的第十二章:二进制加法器.讲述了全加器,半加器的原理以及如何实现加法.实现加法时所使用的全加器,半加器中包含的所有逻辑门在C语言中都有相应的运 ...
- 编程算法 - 不用加减乘除做加法 代码(C)
不用加减乘除做加法 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 写一个函数, 求两个整数之和, 要求在函数体内不得使用+, -, *, /四 ...
- Python 解决面试题47 不用加减乘除做加法
在看<剑指Offer>过程中,面试题47不用加减乘除做加法,给出的思路是使用二进制的异或以及与运算,总之就是使用二进制.但是在使用Python实现的过程中,对于正整数是没有问题的,但是对于 ...
- 剑指offer(48)不用加减乘除做加法
题目描述 写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. 题目分析 不用加减乘除做加法,我第一时间想到的就是用位运算,毕竟计算机是二进制的,所有的操作都是以位运算为基础 ...
- 《剑指offer》第六十五题(不用加减乘除做加法)
// 面试题65:不用加减乘除做加法 // 题目:写一个函数,求两个整数之和,要求在函数体内不得使用+.-.×.÷ // 四则运算符号. #include <iostream> int A ...
- 剑指offer42:不用加减乘除做加法
分析: (1)十进制加法分三步:(以5+17=22为例) 1. 只做各位相加不进位,此时相加结果为12(个位数5和7相加不进位是2,十位数0和1相加结果是1): 2. 做进位,5+7中有进位,进位的值 ...
- 【剑指offer】面试题 65. 不用加减乘除做加法
面试题 65. 不用加减乘除做加法 题目描述 题目:写一个函数,求两个整数之和,要求在函数体内不得使用+.-.*./四则运算符号. Java 实现 public class Solution { ...
随机推荐
- My latest news (--2016.10)
2016.10.31 22:44 一个“程序”,打代码占40%.思考占60% 2016.10.30 20:53 周末,话说今天有晚上讲座,还点名,了,悲催.之前学习的Qt有点问题,悲催.推荐个博文:h ...
- Mastering C# structs
http://www.developerfusion.com/article/84519/mastering-structs-in-c/
- [转]Ubuntu 16.04建议安装
Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...
- HITtrainning20140417题解
题目列表: ID Origin Title 10 / 15 Problem A FZU 2152 文件系统 0 / 16 Problem B FZU 2153 A simple geome ...
- 解析posix与perl标准的正则表达式区别 ---PHP
正则表达式(Regular Expression,缩写为regexp,regex或regxp),又称正规表达式.正规表示式或常规表达式或正规化表示法或正规表示法,是指一个用 来描述或者匹配一系 ...
- github及其他记录
http://mvnrepository.com/artifact/org.jdom/jdom/1.1.3 https://github.com/open-power-workgroup/Hospit ...
- CSS vertical-align 属性
定义和用法 vertical-align 属性设置元素的垂直对齐方式.该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐
- 被拒原因——You have selected the Kids Category for your app, but it does not include the required privacy policy. Please update your app metadata to include a privacy policy URL and ensure that the URL yo
对于一些孩子类的应用,必须加上隐私政策网址(URL),直接截个图吧! 就是你上架的时候,填写应用信息,里面有一个隐私政策网址(URL),望后者不掉坑里了!!!
- Yahoo News Digest(雅虎新闻摘要)APP的推出,未来的seo界又要受伤了
雅虎在 CES 上公布了旗下首款基于 Summly (天才青少年尼克·阿洛伊西奥(Nick D'Aloisio)开发,此前将其公司以3300万美元出售给雅虎)的新闻 APP - Yahoo News ...
- Mac 上真正替换LiveWriter 的工具 - ecto
Mac 上真正替换LiveWriter 的工具 - ecto 13年开始使用mac.而后想把 windows 替换到.一直在寻找LiveWriter 的工具,至今终于找到 我先感谢这位博主 http: ...