-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivideTwoIntegers.cc
72 lines (55 loc) · 1.18 KB
/
DivideTwoIntegers.cc
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
#include <vector>
#include <string>
#include <iostream>
using std::vector;
using std::string;
using std::cout;
using std::endl;
#include <cstdlib>
using std::atoi;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x), next(NULL){}
};
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdint>
class Solution{
public:
int abs(int x){
return x > 0? x: -x;
}
int divide(int dividend, int divisor){
std::int64_t ans = 0;
std::int64_t dvd = dividend;
std::int64_t dvi = divisor;
int positive = 1;
if(dvd < 0){
positive = -positive;
dvd = -dvd;
}
if(dvi < 0){
positive = -positive;
dvi = -dvi;
}
for(int i = 31; i >= 0; --i){
if((dvd >> i) >= dvi){
ans += (1LL << i);
dvd -= (dvi) << i;
}
}
if(positive < 0)ans = -ans;
return (ans > INT_MAX || ans < INT_MIN)? INT_MAX: ans;
}
};
int main(){
Solution slu;
using std::cin;
int x, y;
while(cin >> x >> y){
cout << slu.divide(x, y) << endl;
}
return 0;
}