Problem Description: Quotient, cannot use multiplication, division, modulo operation.
Algorithm idea: Can not use division, that can only use subtraction, but with subtraction, time-out. You can use the displacement operation, each divisor left shift, equivalent to twice times.
1 Public classDividtwointegers {2 Public intDivideintDividend,intdivisor)3 {4 if(divisor = = 0)returnInteger.max_value;5 if(Divisor = =-1 && dividend = = integer.min_value)returnInteger.max_value;6 7 LongPdividend = Math.Abs ((Long) dividend);//take the absolute value, put the overflow into a long8 LongPdivisor = Math.Abs ((Long) divisor);9 Ten intresult = 0; One while(Pdividend >=pdivisor) A { - intCount = 0;//Record Displacement - while(Pdividend >= (pdivisor <<count)) the { -Count + +; - } -Result + = (1 << (count-1)); +Pdividend-= (pdivisor << (count-1)); - } + if((Dividend <0 && divisor < 0) | | (Dividend > 0 && divisor > 0)) A { at returnresult; - } - Else - { - return-result; - } in } - Public Static voidMain (string[] args) to { +Dividtwointegers dt =Newdividtwointegers (); - intA = Dt.divide (10, 3); the System.out.println (a); * } $}
Divide---------numbers, two, divide, multiply, divide, take modulo operations.