# Divide---------numbers, two, divide, multiply, divide, take modulo operations.

Source: Internet
Author: User

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.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.