-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29.cpp
More file actions
61 lines (59 loc) · 1.39 KB
/
Copy path29.cpp
File metadata and controls
61 lines (59 loc) · 1.39 KB
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
#include "head.h"
using namespace std;
class Solution {
public:
int divide(int a, int b) {
if(b==0){return 0;}
if(b==1){return a;}
int sign=-1,res=0;
if(b==-1){
if(a>INT_MIN){return -a;}
return INT_MAX;
}
if((a>0&&b>0)||(a<0&&b<0)){sign=1;}
a=a>0?0-a:a;
b=b>0?0-b:b;
res=div(a,b);
return sign>0?res:0-res;
}
int div(int a,int b){
if(a>b){return 0;}
int count=1,sum=b;
while(sum+sum>=a&&sum+sum<0){
count+=count;
sum+=sum;
}
return count+div(a-sum,b);
}
};
int main(){
Solution so;
cout<<so.divide(-2147483648,2);
// cout<<so.divide(10,3);
}
// class Solution {
// public:
// int divide(int a, int b) {
// int sign=-1,res=0;
// if((a>0&&b>0)||(a<0&&b<0)){sign=1;}
// a=a>0?0-a:a;
// b=b>0?0-b:b;
// if(b==0){return 0;}
// if(a>b){return 0;}
// if(b==-1){
// if(a>INT_MIN){return sign>0?-a:a;}
// return INT_MAX;
// }
// res=div(a,b);
// return sign>0?res:0-res;
// }
// int div(int a,int b){
// if(a>b){return 0;}
// int count=1,sum=b;
// while(sum+sum>=a){
// count+=count;
// sum+=sum;
// }
// return count+div(a-sum,b);
// }
// };