-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp29.cpp
49 lines (43 loc) · 1.07 KB
/
p29.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
#include<algorithm>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
if (dividend==0) return 0;
if (divisor==1) return dividend;
bool negative=false;
negative = (dividend ^ divisor) <0;
unsigned int ans=0;
unsigned int t=dividend>=0?dividend:~dividend+1;
unsigned int d=divisor>=0?divisor:~divisor+1;
if (t<d) return 0;
if (d==1) ans=t;
else
for (int i=31; i>=0;i--) {
if ((t>>i)>=d) {
ans+=1<<i;
t-=d<<i;
}
}
if (ans>(negative?0x80000000U:0x7fffffffU))
return 0x7fffffff;
return negative?~ans+1:ans;
}
};
static const auto io_sync_off = []()
{
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}();
int main() {
auto res = Solution().divide(-2147483648,-2);
cout << res <<endl;
cout << "stop";
return 0;
}