-
Notifications
You must be signed in to change notification settings - Fork 0
/
DivideTwoIntegers.cpp
75 lines (57 loc) · 1.29 KB
/
DivideTwoIntegers.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
74
75
#include<iostream>
#include<cmath>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor) {
int MAX_INT=2147483647,MIN_INT=-2147483648;
if(divisor==0){
return dividend>0?MAX_INT:MIN_INT;
}
if(dividend==MIN_INT&&divisor==-1){
return MAX_INT;
}
long long x=abs((long)dividend),y=abs((long)divisor);
int count=0;
while(x-y>=0){
long long temp=1;
while(x-y>=0){
y<<=1;
temp<<=1;
}
x-=y>>1;
count+=temp>>1;
y=abs((long)divisor);
}
int sign=(dividend>0)^(divisor>0)?-1:1;
return count*sign;
/*for(;x>0;x/=10) m++;
for(;y>0;y/=10) n++;
cout<<m<<endl;
cout<<n<<endl;
int result=0;
x=dividend;
y=divisor;
for(int i=m;i<=n;i++){
int count=0;
int zn=1;
for(int j=0;j<n-m;j++){
x/=10;
zn*=10;
}
while(x-y>=0){
x-=y;
count++;
}
result=result*10+count;
x=x*zn+
}*/
}
};
int main(){
int dividend,divisor;
cin>>dividend>>divisor;
Solution *solution=new Solution();
cout<<solution->divide(dividend,divisor);
return 0;
}