solution.md 6.5 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 分数到小数
2

每日一练社区's avatar
每日一练社区 已提交
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
<p>给定两个整数,分别表示分数的分子 <code>numerator</code> 和分母 <code>denominator</code>,以 <strong>字符串形式返回小数</strong></p>

<p>如果小数部分为循环小数,则将循环的部分括在括号内。</p>

<p class="MachineTrans-lang-zh-CN">如果存在多个答案,只需返回 <strong>任意一个</strong></p>

<p class="MachineTrans-lang-zh-CN">对于所有给定的输入,<strong>保证</strong> 答案字符串的长度小于 <code>10<sup>4</sup></code></p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>numerator = 1, denominator = 2
<strong>输出:</strong>"0.5"
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>numerator = 2, denominator = 1
<strong>输出:</strong>"2"
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>numerator = 2, denominator = 3
<strong>输出:</strong>"0.(6)"
</pre>

<p><strong>示例 4:</strong></p>

<pre>
<strong>输入:</strong>numerator = 4, denominator = 333
<strong>输出:</strong>"0.(012)"
</pre>

<p><strong>示例 5:</strong></p>

<pre>
<strong>输入:</strong>numerator = 1, denominator = 5
<strong>输出:</strong>"0.2"
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>-2<sup>31</sup> <numerator, denominator <= 2<sup>31</sup> - 1</code></li>
	<li><code>denominator != 0</code></li>
</ul>

每日一练社区's avatar
每日一练社区 已提交
57
<p>以下<span style="color:red">错误</span>的选项是?</p>
每日一练社区's avatar
每日一练社区 已提交
58 59

## aop
60

每日一练社区's avatar
每日一练社区 已提交
61
### before
62

每日一练社区's avatar
每日一练社区 已提交
63 64 65 66
```cpp
#include <bits/stdc++.h>
using namespace std;
```
每日一练社区's avatar
每日一练社区 已提交
67

每日一练社区's avatar
每日一练社区 已提交
68
### after
69

每日一练社区's avatar
每日一练社区 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82
```cpp
int main()
{
    Solution sol;
    string res;
    int numerator = 1, denominator = 2;
    res = sol.fractionToDecimal(numerator, denominator);
    cout << res;
    return 0;
}
```

## 答案
83

每日一练社区's avatar
每日一练社区 已提交
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 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
```cpp
class Solution
{
public:
    const static int CACHE_SIZE = 1000;
    string fractionToDecimal(int numerator, int denominator)
    {
        if (numerator == 0)
            return "0";

        bool isNegative = numerator < 0 ^ denominator < 0;
        double doubleNumerator = fabs(numerator);
        double doubleDenominator = fabs(denominator);
        double integral = floor(doubleNumerator / doubleDenominator);
        doubleNumerator -= doubleDenominator * integral;
        int len = 0;
        string cache;
        vector<double> dividendCache;
        bool isRecurring = false;
        int recurringStart = 0;
        int i = 0;
        while (doubleNumerator)
        {
            doubleNumerator *= 10;
            len = dividendCache.size();
            for (i = 0; i < len; ++i)
                if (dividendCache[i] == doubleNumerator)
                {
                    isRecurring = true;
                    recurringStart = i;
                    break;
                }
            if (isRecurring)
                break;
            i = (int)(floor(doubleNumerator / doubleDenominator));
            cache += i;
            dividendCache.push_back(doubleNumerator);
            doubleNumerator -= doubleDenominator * i;
        }

        string ret = isNegative ? "-" : "";
        string tmp;
        char c;
        if (integral == 0)
            ret += "0";
        else
        {
            while (integral)
            {
                c = (int)(integral - 10 * floor(integral / 10)) + '0';
                tmp = c + tmp;
                integral = floor(integral / 10);
            }
            ret += tmp;
        }
        if (dividendCache.size() > 0)
            ret += '.';
        i = 0;
        if (isRecurring)
        {
            ret += cache.substr(0, recurringStart);
            ret += '(';
            ret += cache.substr(recurringStart);
            ret += ')';
        }
        else
            ret += cache;

        return ret;
    }
};
```
## 选项

### A
每日一练社区's avatar
每日一练社区 已提交
159

每日一练社区's avatar
每日一练社区 已提交
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
```cpp
class Solution
{
public:
    string fractionToDecimal(int numerator, int denominator)
    {
        typedef long long LL;
        LL x = numerator, y = denominator;
        if (x % y == 0)
            return to_string(x / y);
        string res;
        if ((x < 0) ^ (y < 0))
            res += '-';
        x = abs(x), y = abs(y);
        res += to_string(x / y) + '.';
        x %= y;
        unordered_map<LL, int> hash;
        while (x)
        {
            hash[x] = res.size();
            x *= 10;
            res += to_string(x / y);
            x %= y;
            if (hash.count(x))
            {
                res = res.substr(0, hash[x]) + '(' + res.substr(hash[x]) + ')';
                break;
            }
        }
        return res;
    }
};
```

### B
每日一练社区's avatar
每日一练社区 已提交
195

每日一练社区's avatar
每日一练社区 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
```cpp
class Solution
{
public:
    string fractionToDecimal(int numerator, int denominator)
    {
        string ans;
        unordered_map<long, int> use;
        if (numerator > 0 && denominator < 0 || numerator < 0 && denominator > 0)
            ans = '-';
        long numerat = fabs(numerator);
        long denominat = fabs(denominator);
        ans += to_string(numerat / denominat);
        numerat = numerat % denominat;
        int point, x;
        if (numerat)
        {
            ans += ".";
            point = ans.length() - 1;
        }
        else
            return ans;
        while (numerat && use.count(numerat) == 0)
        {
            use[numerat] = ++point;
            numerat *= 10;
            x = numerat / denominat;
            numerat = numerat % denominat;
            ans.push_back(x + '0');
        }
        if (numerat)
        {
            ans.insert(ans.begin() + use[numerat], '(');
            ans.push_back(')');
        }
        return ans;
    }
};
```

### C
每日一练社区's avatar
每日一练社区 已提交
237

每日一练社区's avatar
每日一练社区 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
```cpp
class Solution
{
public:
    string fractionToDecimal(int numerator, int denominator)
    {
        int m1 = numerator > 0 ? 1 : -1;
        int m2 = denominator > 0 ? 1 : -1;
        long long num = abs((long long)numerator);
        long long den = abs((long long)denominator);
        long long t = num / den;
        long long res = num % den;
        string ans = to_string(t);
        if (m1 * m2 == -1 && (t > 0 || res > 0))
            ans = '-' + ans;
        if (res == 0)
            return ans;
        ans += '.';
        unordered_map<long long, int> m;
        string s = "";
        int pos = 0;
        while (res != 0)
        {
            if (m.find(res) != m.end())
            {
                s.insert(m[res], "(");
                s += ')';
                return ans + s;
            }
            m[res] = pos;
            s += to_string((res * 10) / den);
            res = (res * 10) % den;
            pos++;
        }
        return ans + s;
    }
};
```