forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
56 lines (51 loc) · 1.38 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int divide(int dividend, int divisor) {
if (divisor == 0) {
return INT_MAX;
}
if (dividend == INT_MIN) {
if (divisor == -1) {
return INT_MAX;
} else if (divisor == 1) {
return INT_MIN;
}
}
if (dividend == INT_MAX) {
if (divisor == 1) {
return INT_MAX;
} else if (divisor == -1) {
return -dividend;
}
}
long long first = (long long) dividend;
long long second = (long long) divisor;
int sign = (first >= 0 && second > 0) || (first < 0 && second < 0);
sign = sign == 0 ? -1 : sign;
first = llabs(first);
second = llabs(second);
int res = 0;
while (first >= second) {
long long i = 0;
while (first > (second << (i + 1))) {
i += 1;
}
res += (1 << i);
first = first - (second << i);
}
return res * sign;
}
int main(int argc, char const *argv[]) {
printf("%d\n", divide(36, 3));
printf("%d\n", divide(-36, 3));
printf("%d\n", divide(38, 3));
printf("%d\n", divide(2, 3));
printf("%d\n", divide(-38, 3));
printf("%d\n", divide(-39, 3));
printf("%d\n", divide(0, 3));
printf("%d\n", divide(4, 2));
printf("%d\n", divide(2147483647, 2));
printf("%d\n", divide(-2147483648, 2));
return 0;
}