Realize A+B , And Thinking of recursion
来源:互联网 发布:亚历克斯·张伯伦 知乎 编辑:程序博客网 时间:2024/06/15 05:25
问题描述:
给出两个整数a和b, 求他们的和, 但不能使用 +
等数学运算符。
思考:不能应用数学运算符,但是实现运算,则使用底层的二进制机器语言的计算,位运算符
则&(按位与)、|(按位或)、^(按位异或)、~ (按位取反)。
且进位的方法为(a&b)<<1;
且递归的实现方法为函数,一般为函数的返回值又再次被函数利用,如此递归,直至求出结果,参考二分查找算法
实现方法:
#include <iostream>using namespace std;int aplusb ( int a , int b);int main(){ int a = 5; int b = 6; int c = 0; c = aplusb (a , b); cout << c;}int aplusb ( int a , int b){ if ( a == 0 ) return b ; if ( b == 0 ) return a ; int sum , i ; i = a ^ b ; sum = (a & b) << 1 ; return aplusb ( sum , i);}
阅读全文
0 0
- Realize A+B , And Thinking of recursion
- print all permutation of a string(recursion)
- one of methods of query table a and table b
- Tail recursion and normal recursion
- Review of Codeforces 5A and 5B(Python)
- codeforces 850B Arpa and a list of numbers
- Codeforces Round #432 B. Arpa and a list of numbers
- Realize
- give two sorted array, find the k-th smallest element of union of A and B
- and is supported by 2GB of RAM b It has a battery life of 2
- Example of Recursion.
- Four rules of recursion
- the operation of recursion
- FileDirectory And Recursion
- asymptotic notation and recursion
- Exhaustive recursion and backtracking
- iteration and recursion
- B. A and B and Compilation Errors
- Webview调用相册相机失败等问题(h5交互问题)
- 框架中的部分源码替代写法
- 第五次模拟题(DNA)
- iOS-Block的使用
- Java大数据分析图的十大错误,你占了几个?
- Realize A+B , And Thinking of recursion
- Android群英传学习——第三章、Android控件架构与自定义控件详解
- 二叉查找树(BST) | 平衡二叉查找树(AVL) | 红黑树(RBT)
- JS传后台中文乱码问题
- iOS开发之---block的使用(详解)
- vue饿了么学习-第六篇(mock数据)
- 《算法》第四版第一章以及java核心技术卷I基础部分知识点
- double vs long double
- maven 快照 更新策略