两数相除
给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。
返回被除数 dividend 除以除数 divisor 得到的商。
整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2
提示:
被除数和除数均为 32 位有符号整数。
除数不为 0。
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/divide-two-integers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
import java.util.Scanner;
public class 两数相除 {
//题解解法
public static int divide(int dividend, int divisor) {
if(dividend == 0) return 0;
if(divisor == 1) return dividend;
if(divisor == -1) {
if(dividend > Integer.MIN_VALUE) return -dividend;
return Integer.MAX_VALUE;
}
long a = dividend;
long b = divisor;
int sign = 1;
if((a>0&&b<0) || (a<0&&b>0)){
sign = -1;
}
a = a>0?a:-a;
b = b>0?b:-b;
long res = div(a,b);
if(sign > 0) return res>Integer.MAX_VALUE?Integer.MAX_VALUE: (int) res;
return (int) -res;
}
private static int div(long a, long b) {
if(a<b) return 0;
long count = 1;
long tb = b;
while((tb+tb) <= a){
count = count + count;
tb = tb + tb;
}
return (int) (count + div(a-tb,b));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dividend = sc.nextInt();
int divisor = sc.nextInt();
int res = divide(dividend,divisor);
System.out.println(res);
}
}