当前位置:网站首页>20220605数学:两数相除
20220605数学:两数相除
2022-07-03 09:20:00 【丿SeeYouAgain】
题目描述:给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。返回被除数 dividend 除以除数 divisor 得到的商。整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2。
编码实现:
public int divide(int dividend, int divisor) {
int result = 0;
int minValue = Integer.MIN_VALUE,maxValue = Integer.MAX_VALUE;
if (dividend == 0){
return 0;
} else if(dividend == minValue && divisor == -1){
return maxValue;
} else if(dividend == minValue && divisor == 1){
return minValue;
} else if(dividend == minValue && divisor == minValue){
return 1;
} else if(divisor == minValue){
return 0;
}
boolean negative = (dividend ^ divisor) < 0;
if(dividend == minValue) {
dividend += Math.abs(divisor);
result++;
}
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
for(int i = 31; i >= 0; i--) {
if((dividend >> i) >= divisor) {
result += 1 << i;
dividend -= divisor << i;
}
}
if(result == minValue){
return maxValue;
} else{
return negative ? -result : result;
}
}
边栏推荐
- Drive and control program of Dianchuan charging board for charging pile design
- MySQL root user needs sudo login
- Positive and negative sample division and architecture understanding in image classification and target detection
- Gpiof6, 7, 8 configuration
- 2021-01-03
- Stm32 NVIC interrupt priority management
- Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
- Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
- Opencv notes 17 template matching
- Qcombox style settings
猜你喜欢

LeetCode - 5 最长回文子串

LeetCode - 1670 設計前中後隊列(設計 - 兩個雙端隊列)

2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
![[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer](/img/90/4de927e797ec9c2bb70e507392bed0.jpg)
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer

Timer and counter of 51 single chip microcomputer

Retinaface: single stage dense face localization in the wild

QT is a method of batch modifying the style of a certain type of control after naming the control

Notes on C language learning of migrant workers majoring in electronic information engineering

STM32 interrupt switch
![[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)](/img/19/5dc152b3fadeb56de50768561ad659.jpg)
[combinatorics] Introduction to Combinatorics (combinatorial idea 3: upper and lower bound approximation | upper and lower bound approximation example Remsey number)
随机推荐
[untitled] proteus simulation of traffic lights based on 89C51 Single Chip Microcomputer
RESNET code details
Sending and interrupt receiving of STM32 serial port
Cases of OpenCV image enhancement
Working mode of 80C51 Serial Port
LeetCode - 706 设计哈希映射(设计) *
Application of external interrupts
Crash工具基本使用及实战分享
(1) What is a lambda expression
Opencv histogram equalization
Window maximum and minimum settings
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
2020-08-23
LeetCode - 673. 最长递增子序列的个数
CV learning notes ransca & image similarity comparison hash
51 MCU tmod and timer configuration
LeetCode - 933 最近的请求次数
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
getopt_ Typical use of long function
My openwrt learning notes (V): choice of openwrt development hardware platform - mt7688