当前位置:网站首页>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;
}
}
边栏推荐
- Liquid crystal display
- LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
- Tensorflow2.0 save model
- LeetCode - 706 设计哈希映射(设计) *
- Serial port programming
- The data read by pandas is saved to the MySQL database
- Wireshark use
- 01仿B站项目业务架构
- Leetcode - 933 number of recent requests
- Of course, the most widely used 8-bit single chip microcomputer is also the single chip microcomputer that beginners are most easy to learn
猜你喜欢
There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
Pymssql controls SQL for Chinese queries
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))
Yocto Technology Sharing Phase 4: Custom add package support
el-table X轴方向(横向)滚动条默认滑到右边
LeetCode - 705 设计哈希集合(设计)
Adaptiveavgpool1d internal implementation
Stm32f407 key interrupt
Serial communication based on 51 single chip microcomputer
随机推荐
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)
Leetcode - 460 LFU cache (Design - hash table + bidirectional linked hash table + balanced binary tree (TreeSet))*
Serial port programming
Cases of OpenCV image enhancement
LeetCode - 460 LFU 缓存(设计 - 哈希表+双向链表 哈希表+平衡二叉树(TreeSet))*
4G module board level control interface designed by charging pile
2021-11-11 standard thread library
Do you understand automatic packing and unpacking? What is the principle?
LeetCode - 900. RLE 迭代器
Timer and counter of 51 single chip microcomputer
CV learning notes - reasoning and training
getopt_ Typical use of long function
Opencv feature extraction sift
Exception handling of arm
Development of intelligent charging pile (I): overview of the overall design of the system
Octave instructions
pycharm 无法引入自定义包
Installation and removal of MySQL under Windows
2021-01-03