forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_binary_nlogn.cpp
47 lines (40 loc) · 1022 Bytes
/
AC_binary_nlogn.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_binary_nlogn.cpp
* Create Date: 2015-01-16 09:42:12
* Descripton: Binary. Just use the bit.
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
typedef long long ll;
class Solution {
public:
int divide(int dividend, int divisor) {
ll a = dividend >= 0 ? dividend : -(ll)dividend;
ll b = divisor >= 0 ? divisor : -(ll)divisor;
ll result = 0, c = 0;
bool sign = (dividend > 0 && divisor < 0) ||
(dividend < 0 && divisor > 0);
while (a >= b) {
c = b;
for (int i = 0; a >= c; i++, c <<= 1) {
a -= c;
result += (1<<i);
}
}
if (sign) {
return max((ll)INT_MIN, -result);
} else {
return min((ll)INT_MAX, result);
}
}
};
int main() {
int a, b;
Solution s;
while (cin >> a >> b) {
cout << s.divide(a, b) << endl;
}
return 0;
}