-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdivide.cpp
73 lines (45 loc) · 1.35 KB
/
divide.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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
if(dividend == INT_MIN && divisor==-1) return INT_MAX;
if(dividend == INT_MIN && divisor == 1) return INT_MIN;
long long int a = abs(dividend);
long long int b = abs(divisor);
int ans = 0;
bool f = true;
bool s = true;
if(a<b){
return ans;
}
if(dividend<0)
f = false;
if(divisor<0)
s = false;
bool positive=f^s;
while(a>=b){
int i=0;
long long t = b<<i;
while(t<a){
i++;
t = b<<i;
}
i--;
if(i<0) i=0;
ans+=(1<<i);
a = a - (b<<i);
}
if(positive){
return -ans;
}
return ans;
}
};
int main() {
// vector<string> nums = {"flower","flow","flight"};
// int val = 121;
Solution obj;
cout<<obj.divide(10,3)<<endl;
return 0;
}