forked from dscmsit/Problem-Solving-in-any-Language
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivide.cpp
More file actions
31 lines (28 loc) · 777 Bytes
/
divide.cpp
File metadata and controls
31 lines (28 loc) · 777 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define rep(int, i, z) for (int i = 0; i < z; i++)
class Solution {
public:
int divide(int dividend, int divisor) {
if(dividend == INT_MIN && divisor == -1)return INT_MAX;
long ans = 0;
int sign = 1;
if(divisor < 0)sign*=-1;
if(dividend <0)sign*=-1;
long dend=labs(dividend);
long sor=labs(divisor);
while(sor <= dend){
long temp=sor;
long pow=1;
while(temp+temp<dend){
temp+=temp;
pow+=pow;
}
ans+=pow;
dend-=temp;
}
return ans*sign;
}
};