solution.md 5.1 KB
Newer Older
1
# 两数相除
F
fix bug  
feilong 已提交
2

3
<p>给定两个整数,被除数&nbsp;<code>dividend</code>&nbsp;和除数&nbsp;<code>divisor</code>。将两数相除,要求不使用乘法、除法和 mod 运算符。</p><p>返回被除数&nbsp;<code>dividend</code>&nbsp;除以除数&nbsp;<code>divisor</code>&nbsp;得到的商。</p><p>整数除法的结果应当截去(<code>truncate</code>)其小数部分,例如:<code>truncate(8.345) = 8</code> 以及 <code>truncate(-2.7335) = -2</code></p><p>&nbsp;</p><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong> dividend = 10, divisor = 3<strong><br />输出:</strong> 3<strong><br />解释: </strong>10/3 = truncate(3.33333..) = truncate(3) = 3</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> dividend = 7, divisor = -3<strong><br />输出:</strong> -2<strong><br />解释:</strong> 7/-3 = truncate(-2.33333..) = -2</pre><p>&nbsp;</p><p><strong>提示:</strong></p><ul>	<li>被除数和除数均为 32 位有符号整数。</li>	<li>除数不为&nbsp;0。</li>	<li>假设我们的环境只能存储 32 位有符号整数,其数值范围是 [&minus;2<sup>31</sup>,&nbsp; 2<sup>31&nbsp;</sup>&minus; 1]。本题中,如果除法结果溢出,则返回 2<sup>31&nbsp;</sup>&minus; 1。</li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
4
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27
```cpp
int main()
{
    Solution sol;
    int res;
    res = sol.divide(10, 3);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
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
```cpp
class Solution
{
public:
    int divide(int dividend, int divisor)
    {
        if (dividend == 0 || divisor == 1)
            return dividend;
        if (divisor == -1)
            return dividend == INT_MIN ? INT_MAX : -dividend;
        int sign = (dividend > 0) ^ (divisor > 0) ? -1 : 1;
        int res = div(-abs(dividend), -abs(divisor));
        return sign == 1 ? res : -res;
    }
    int div(int a, int b)
    {
        if (a > b)
            return 0;
        int cnt = 1, val = b;
        while (val - a + val >= 0)
        {
            cnt <<= 1;
            val += val;
        }
        return div(a - val, b);
    }
};
```
## 选项

F
fix bug  
feilong 已提交
59

60
### A
F
fix bug  
feilong 已提交
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
```cpp
class Solution
{
public:
    int divide(int dividend, int divisor)
    {
        int signal = 1;
        unsigned int dvd = dividend;
        if (dividend < 0)
        {
            signal *= -1;
            dvd = ~dvd + 1;
        }
        unsigned int dvs = divisor;
        if (divisor < 0)
        {
            signal *= -1;
            dvs = ~dvs + 1;
        }
        int shift = 0;
        while (dvd > dvs << shift)
        {
            shift++;
        }
        unsigned int res = 0;
        while (dvd >= dvs)
        {
            while (dvd < dvs << shift)
            {
                shift--;
            }
            res |= (unsigned int)1 << shift;
            dvd -= dvs << shift;
        }
        if (signal == 1 && res >= INT_MAX)
        {
            return INT_MAX;
        }
        else
        {
            return res * signal;
        }
    }
};
```

### B
F
fix bug  
feilong 已提交
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
```cpp
class Solution
{
public:
    int divide(int dividend, int divisor)
    {
        if (dividend == INT_MIN && divisor == -1)
            return INT_MAX;
        if (dividend == 0)
            return 0;
        int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
        long x = (dividend < 0) ? -(long)dividend : (long)dividend;
        long y = (divisor < 0) ? -(long)divisor : (long)divisor;
        long result = 0;
        while (x >= y)
        {
            long temp = y, mid = 1;
            while (x >= (temp << 1))
            {
                mid <<= 1;
                temp <<= 1;
            }
            result += mid;
            x -= temp;
        }
        return sign > 0 ? result : -result;
    }
};
```

### C
F
fix bug  
feilong 已提交
141

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
```cpp
class Solution
{
public:
    int divide(int dividend, int divisor)
    {
        int INI_MIN = -2147483648, INI_MAX = 2147483647;
        if (dividend == divisor)
            return 1;
        if (divisor == 1)
            return dividend;
        if (dividend == INI_MIN && divisor == -1)
            return INI_MAX;
        if (divisor == INI_MIN)
        {
            if (dividend == INI_MIN)
                return 1;
            else
                return 0;
        }

        bool sign = false;
        if (dividend > 0 && divisor > 0 || dividend < 0 && divisor < 0)
        {
            sign = true;
        }

        int result_1 = 0;
        if (dividend == INI_MIN)
        {
            if (divisor > 0)
                dividend = dividend + divisor;
            else
                dividend = dividend - divisor;
            result_1++;
        }

        dividend = abs(dividend);
        divisor = abs(divisor);
        while (dividend >= divisor)
        {
            unsigned int temp = divisor, res = 1;

            while (dividend >= (temp << 1))
            {
                res <<= 1;
                temp <<= 1;
            }

            result_1 += res;
            dividend -= temp;
        }
        if (sign == true)
        {
            return result_1;
        }
        else
        {
            return -result_1;
        }
    }
};
```