-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide_two_integers.cpp
88 lines (80 loc) · 2.19 KB
/
divide_two_integers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* =====================================================================================
*
* Filename: divide_two_integers.cpp
*
* Description: Divide Two Integers: 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.
*
* Version: 1.0
* Created: 08/02/18 01:50:59
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), [email protected]
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
class Solution
{
public:
int divide(int dividend, int divisor)
{
bool minus = (dividend ^ divisor) < 0 ? true : false;
unsigned int x = ((dividend < 0) ? (~dividend + 1) : dividend);
unsigned int y = ((divisor < 0) ? (~divisor + 1) : divisor);
unsigned int z = 0;
if (x == y)
{
z = 1;
}
else if (x > y)
{
do {
int n = 0;
while (x >= (y << 1))
{
n += 1;
y <<= 1;
if (x == y)
{
break;
}
}
// printf("%x, %x, ", x, y);
x -= y;
y = ((divisor < 0) ? (~divisor + 1) : divisor);
z |= (1 << n);
// printf("%x, %x, %x\n", x, y, z);
} while (x >= y);
}
if (minus)
{
return -(int)z;
}
else if (z > INT_MAX)
{
return INT_MAX;
}
return z;
}
};
int main(int argc, char* argv[])
{
int dividend = 10;
int divisor = 3;
if (argc == 3)
{
dividend = atoi(argv[1]);
divisor = atoi(argv[2]);
}
int quotient = Solution().divide(dividend, divisor);
printf("%d / %d = %d\n", dividend, divisor, quotient);
return 0;
}