当前位置:网站首页>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;
}
}
边栏推荐
- Blue Bridge Cup for migrant workers majoring in electronic information engineering
- Dictionary tree prefix tree trie
- Pycharm cannot import custom package
- Wireshark use
- 4G module at command communication package interface designed by charging pile
- The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving
- 使用sed替换文件夹下文件
- STM32 running lantern experiment - library function version
- 2020-08-23
- Retinaface: single stage dense face localization in the wild
猜你喜欢
LeetCode - 705 设计哈希集合(设计)
My notes on intelligent charging pile development (II): overview of system hardware circuit design
LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)
Yocto technology sharing phase IV: customize and add software package support
03 FastJson 解决循环引用
CV learning notes ransca & image similarity comparison hash
CV learning notes - image filter
使用密钥对的形式连接阿里云服务器
03 fastjason solves circular references
Adaptiveavgpool1d internal implementation
随机推荐
QT self drawing button with bubbles
About windows and layout
Opencv notes 17 template matching
LeetCode - 673. Number of longest increasing subsequences
4G module IMEI of charging pile design
2021-11-11 standard thread library
One click generate traffic password (exaggerated advertisement title)
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
Toolbutton property settings
Gpiof6, 7, 8 configuration
yocto 技術分享第四期:自定義增加軟件包支持
Basic knowledge of communication interface
Basic use and actual combat sharing of crash tool
2020-08-23
CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)
I think all friends should know that the basic law of learning is: from easy to difficult
Crash工具基本使用及实战分享
Tensorflow2.0 save model
Pycharm cannot import custom package
The new series of MCU also continues the two advantages of STM32 product family: low voltage and energy saving