-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivide_two_integers.cpp
86 lines (74 loc) · 2.02 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
#include <cassert>
#include <iostream>
class Solution {
public:
int divide(int dividend, int divisor) {
const int N = 32;
int sign = 1;
long long int dend = dividend;
long long int mod = divisor;
if (mod == -2147483648) {
if (dend == mod)
return 1;
else
return 0;
}
if (mod < 0) {
mod = -mod;
dend = -dend;
}
if (dend < 0)
sign = -1;
mod <<= N;
long long int ret = 0;
for (int i = 0; i < N; i++) {
mod >>= 1;
ret <<= 1;
if (dend >= 0) {
dend -= mod;
ret += 1;
} else {
dend += mod;
ret -= 1;
}
}
assert(-mod <= dend);
assert(dend <= mod);
if (sign == 1) {
if (dend == mod) {
ret += 1;
dend = 0;
}
if (dend < 0)
ret -= 1;
} else {
if (dend == -mod) {
ret -= 1;
dend = 0;
}
if (dend > 0)
ret += 1;
}
return ret;
}
};
#define test(a, b) assert((a) / (b) == s.divide(a, b))
int main()
{
Solution s;
test(13,3);
int i = -2147483648 / -3;
test(-2147483648, -3);
test(-2147483648, 1);
test(0, 2);
test(1, 2);
test(2, 2);
test(3, 2);
test(4, 2);
test(5, 2);
test(6, 2);
test(0, 1);
test(-2147483648, -2);
test(-1010369383, -2147483648);
return 0;
}