Leetcode divide two integers

Source: Internet
Author: User

Divide two integers without using multiplication, division and mod operator.

Returns the result of division of two integers without multiplication, division, and division. The result is also an integer.

If the divisor is 2, the Division operator is that the divisor binary represents moving one digit to the right.

Assume that the divisor is a and the divisor is B. because we do not know the quotient that A is divided by B, we can only try this sequence one by one from B, 2B, 4B, 8B ......

Deduct the values of the attempts from.

If A is greater than the number of sequences, A deducts this value, and the final result is that the quotient is added with the corresponding binary bit 1, and then tries the next number of sequences.

If a is less than the number of sequences, start from scratch until a is less than B.

 

 Class  Solution { Public  :  Int Divide ( Int Dividend, Int  Divisor ){  Long   Long A = llabs (( Long   Long ) Dividend), B = llabs (( Long   Long ) (Divisor), result = 0  ;  While (A> = B ){  Long   Long C = B;  Int I = 0  ;  While (A> = C) { -= C; Result + = 1 <I; ++ I; c = C < 1 ;}}  If (Dividend> 0 & Divisor < 0 ) | (Dividend < 0 & Divisor> 0 ) Result =- Result;  Return ( Int  ) Result ;}}; 

 

 

 

 

Contact Us

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.