Divide Two Numbers Without Symbol And Also Minus And Multiply
来源:互联网 发布:什么是网络设计 编辑:程序博客网 时间:2024/05/01 06:25
public int divide(int dividend, int divisor) { if(dividend == 0) return 0; if(divisor == 0) return (dividend > 0) ? Integer.MAX_VALUE : Integer.MIN_VALUE; boolean flag = (dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0); long a = Math.abs((long)dividend); long b = Math.abs((long)divisor); //if b is less than a, then move the highest digit of b to maybe the same with a //if a is less than b, then moveTimes will be 0 int moveTimes = 0; for(; a >= (b << 1); moveTimes++) { b <<= 1; } int result = 0; for(; moveTimes >= 0; moveTimes--){ if(a >= b){ a -= b; result += (1 << moveTimes); } b >>= 1; } if(flag) return result; else return -result; } //minus two numbers without minuspublic int minus(int a, int b) {return a + help(b);}public int help(int a) {int result = 0;int tmp = (a < 0) ? 1 : -1;while(a != 0) {result += tmp;a += tmp;}return result;}//multiply two numbers without multiplypublic int multiply(int a, int b) {if(a < b) return multiply(b, a);int result = 0;for(int i = abs(b); i > 0; i--) {result += a;}if(b < 0) return help(result);return result;}public int abs(int a) {if(a < 0) return help(a);return a;}
- Divide Two Numbers Without Symbol And Also Minus And Multiply
- Compare two numbers Without if-else and comparison
- LeeCode And Two Numbers
- [LeetCode] Add Two Numbers、Divide Two Integers、Multiply Strings、Add Binary、Plus One
- Two question about Divide and Conquer
- Symbol xxx multiply defined (by xxx1.o and xxx2.o).
- Two big numbers to multiply
- Words and Their Stories: Numbers Part Two
- 【算法】【Divide and conquer】Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays(divide and conquer)
- Add, Subtract, Multiply numbers without using "+", "-", "*" operation
- Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally
- add two numbers without using +
- MDK编译错误 Symbol SysTick_Handler multiply defined (by hal_cm3.o and xxx.o) 的解决
- Error: L6200E: Symbol B_DisCnctRelayTime multiply defined (by cdma_gps_hc.o and main.o).
- .\Flash\Blinky.axf: Error: L6200E: Symbol SysTick_Handler multiply defined (by hal_cm0.o and blinky.
- Error: L6200E: Symbol SysTick_Handler multiply defined (by stm32f10x_it.o and m
- Divide and conquer algorithm
- 用Eclipse工作集来整合工程
- 『转』黑社会老大儿子的请假条 绝对够牛!
- 利用unordered_map代替hash_map
- JUnit 单元
- 相对位置
- Divide Two Numbers Without Symbol And Also Minus And Multiply
- Julia编程语言
- 函数调用约定
- php5.3 不支持 session_register() 此函数已启用的解决方法
- 内存数据库T-tree索引
- php获得当前的脚本网址
- Windows 7 SP1 x64 旗舰版 微软官方安装U盘的制作
- 【JavaSE进阶】异常的定义、使用和处理
- Struts2入门