-
Notifications
You must be signed in to change notification settings - Fork 0
/
c.py
31 lines (27 loc) · 857 Bytes
/
c.py
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
# Given two integers dividend and divisor,
# divide two integers without using multiplication,
# division, and mod operator.
# Return the quotient after dividing dividend by divisor.
# The integer division should truncate toward zero, which
# means losing its fractional part. For example,
# truncate(8.345) = 8 and truncate(-2.7335) = -2.
# CODE
flag = (dividend < 0) ^ (divisor < 0)
u = abs(dividend)
d = abs(divisor)
m = 1
nd = d
while nd<<1 <= 1<<31:
nd <<= 1
m <<= 1
ans = 0
while nd >= d:
if u >= nd:
ans += m
u -= nd
m >>= 1
nd >>= 1
if flag:
return max(-1<<31, min((1<<31)-1, -ans))
else:
return max(-1<<31, min((1<<31)-1, ans))