Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to divide two numbers in C++

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how C++ divides two numbers". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Divide Two Integers division of two numbers

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3

Output: 3

Example 2:

Input: dividend = 7, divisor =-3

Output:-2

Note:

Both dividend and divisor will be 32-bit signed integers.

The divisor will never be 0.

Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [− 231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

This problem allows us to divide two numbers, and stipulates that multiplication, division and remainder operations cannot be used, then here we can use another artifact bit to operate Bit Manipulation, the idea is, if the divisor is greater than or equal to the divisor, then carry on the following cycle, define the variable t equal to the divisor, define the count p, when the twice of t is less than equal to the divisor, carry on the following loop, t doubles, p doubles, and then updates res and m. The OJ of this question gives some test case is very annoying, because the input is int type, for example, the divisor is-2147483648, in the range of int, when the divisor is-1, the result is beyond the range of int and need to return INT_MAX, so for this case, we start to use if to determine, put it together with the divisor of 0, and return INT_MAX. Then, the positive or negative of the return value is determined according to the positive or negative of the divisor and the divisor. Here, the long integer long is used to complete all the calculations, and finally the returned value is multiplied by the symbol. The code is as follows:

Solution 1:

Class Solution {public: int divide (int dividend, int divisor) {if (dividend = = INT_MIN & & divisor =-1) return INT_MAX; long m = labs (dividend), n = labs (divisor), res = 0; int sign = (dividend

< 0) ^ (divisor < 0)) ? -1 : 1; if (n == 1) return sign == 1 ? m : -m; while (m >

= n) {long t = n, p = 1; while (m > = (t)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report