-
Notifications
You must be signed in to change notification settings - Fork 0
/
191118-1.cpp
56 lines (54 loc) · 1.52 KB
/
191118-1.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
// https://leetcode-cn.com/problems/divide-two-integers/
#include <cstdio>
#include <climits>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend == divisor) return 1;
if (dividend == 0 || divisor == INT_MIN) return 0;
int value = 0;
if (dividend == INT_MIN) {
if (divisor > 0) {
value = -1;
dividend += divisor;
} else {
value = 1;
dividend -= divisor;
}
}
bool minus = (dividend < 0) != (divisor < 0);
if (dividend < 0) dividend = -dividend;
if (divisor < 0) divisor = -divisor;
int res = 0;
for (int i = 31; i >= 0; --i) {
int x = (dividend >> i);
res = res << 1;
if (x >= divisor) {
++res;
dividend -= (divisor << i);
}
}
if (minus) res = -res;
if (res == INT_MAX && value > 0) return INT_MAX;
return res + value;
}
};
int main()
{
Solution s;
printf("%d\n", s.divide(10, 3)); // answer: 3
printf("%d\n", s.divide(7, -3)); // answer: -2
printf("%d\n", s.divide(0, 1)); // answer: 0
printf("%d\n", s.divide(0, -1)); // answer: 0
printf("%d\n", s.divide(1, 1)); // answer: 1
printf("%d\n", s.divide(1, -1)); // answer: -1
printf("%d\n", s.divide(-1, 1)); // answer: -1
printf("%d\n", s.divide(INT_MIN, 1)); // answer: INT_MIN
printf("%d\n", s.divide(INT_MIN, -1)); // answer: INT_MIN
printf("%d\n", s.divide(INT_MIN, INT_MIN)); // answer: 1
printf("%d\n", s.divide(0, INT_MIN)); // answer: 0
printf("%d\n", s.divide(1, INT_MIN)); // answer: INT_MIN
printf("%d\n", s.divide(-1, INT_MIN)); // answer: INT_MIN
return 0;
}