当前位置:网站首页>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;
}
}
边栏推荐
- Serial communication based on 51 single chip microcomputer
- Circular queue related design and implementation reference 1
- The data read by pandas is saved to the MySQL database
- Vgg16 migration learning source code
- 2020-08-23
- Timer and counter of 51 single chip microcomputer
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
- CV learning notes ransca & image similarity comparison hash
- yocto 技术分享第四期:自定义增加软件包支持
- Opencv notes 20 PCA
猜你喜欢

openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹

LeetCode - 508. Sum of subtree elements with the most occurrences (traversal of binary tree)

2.Elment Ui 日期选择器 格式化问题

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

2. Elment UI date selector formatting problem

Octave instructions

Opencv feature extraction - hog

LeetCode - 933 最近的请求次数

Development of intelligent charging pile (I): overview of the overall design of the system

Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
随机推荐
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Installation and removal of MySQL under Windows
2312、卖木头块 | 面试官与狂徒张三的那些事(leetcode,附思维导图 + 全部解法)
2021-01-03
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
Open Euler Kernel Technology Sharing - Issue 1 - kdump Basic Principles, use and Case Introduction
openEuler kernel 技術分享 - 第1期 - kdump 基本原理、使用及案例介紹
Openeuler kernel technology sharing - Issue 1 - kdump basic principle, use and case introduction
Design of charging pile mqtt transplantation based on 4G EC20 module
Connect Alibaba cloud servers in the form of key pairs
Timer and counter of 51 single chip microcomputer
Swing transformer details-2
Leetcode 300 longest ascending subsequence
Leetcode - 1670 conception de la file d'attente avant, moyenne et arrière (conception - deux files d'attente à double extrémité)
El table X-axis direction (horizontal) scroll bar slides to the right by default
is_ power_ of_ 2 judge whether it is a multiple of 2
STM32 running lantern experiment - library function version
Simulate mouse click
CV learning notes - scale invariant feature transformation (SIFT)
Wireshark use