分數化循環小數(純/混)

網上看到這個問題,查了一下除了字符串查找沒看到別的解法,自己寫了一個,要考慮一些特殊情況,比如除0,負數等等,時間複雜度n^2,小數點長度N大於一萬就慢了,感覺哪裏還能再優化一下,最多跑過11111111/59595961,一共591萬位,release要跑20秒5G內存,再大內存就不夠了。

寫的亂七八糟的,不寫點註釋以後估計自己都看不懂。

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int upperLimit = 10;//小數點位數上限,當達到上限仍未找到循環節時乘以三繼續尋找。
    int loopLen = 1;//循環節長度
    __int64 num;
    __int64 quotient;//商的整數部分
    __int64 a, b;
    vector<int> rem;//remainder小數部分
    cin >> a >> b;
    if (b == 0) {
        cout << "Integer division by zero。"; 
        ::system("pause"); 
        return 0;
    }
    if (a == 0) {
        cout << "0"; 
        ::system("pause"); 
        return 0;
    }
    if (float(a) / float(b) < 0) cout << "-";//負數判斷
    a = abs(a);
    b = abs(b);
    quotient = a / b;
    a -= quotient * b;
    while (upperLimit *= 3,1) {
        auto HPC = [&]() {//高精度除法,除至第upperLimit位
            while (rem.size() < upperLimit) {
            a *= 10;
            num = a / b;
            rem.push_back(num);
            a -= b * num;
        }};
        auto Validate = [&]() {//結果有可能爲0.(11...112)類似情況,至少驗證多一倍的長度加上100位
        //以確保不會得出0.(1)這種答案,當然存在有一百多位的0.(11...112)循環,但估計分子分母是寫不下了
            upperLimit *= 2 + 100;
            HPC();
            int front = rem.size() - 2 * loopLen;
            int back = rem.size() - loopLen;
            for (int i = upperLimit / 2; i < upperLimit; i++) {
                if (rem[i] != rem[front + (i - front) % loopLen]) return false;
            }
            return true;
        };
        auto SearchRepetation = [&]() {
            HPC();
            for (; loopLen <= rem.size() / 3; loopLen++) {
                auto HasRepeat = [&]() {
                    for (int index = 1; index <= loopLen; index++)
                        if (rem[rem.size() - index] != rem[rem.size() - loopLen - index] 
                        || rem[rem.size()-index-loopLen]!= rem[rem.size() - index - 2*loopLen]) 
                        return false;
                        //查找時確保循環節出現至少三次,其實兩次就夠了,更多次數也不能保證特殊情況,
                        //主要靠上面的驗證步驟。上面的Validate多出來的一倍和100位也可以合併到這裏面。
                    return true;
                };
                if (HasRepeat() && Validate()) {//確定結果,輸出
                    int front = rem.size() - 2 * loopLen;
                    int back = rem.size() - loopLen;
                    while (front > 0 && rem[front-1] == rem[back-1]) { front--; back--; }
                    if (back - front == 1 && rem[front] == 0) {//無循環
                        if (front != 0)
                            cout << quotient << ".";
                        else//整數情況
                            cout << quotient;
                        for (int i = 0; i < front; i++) {
                            cout << rem[i];
                        }
                    }
                    else {//有循環
                        cout << quotient << ".";
                        for (int i = 0; i < back; i++) {
                            if (i == front)cout << "(";
                            cout << rem[i];
                        }
                        cout << ")";
                    }
                    return true;
                }
            }
            return false;
        };
        if (SearchRepetation()) break;
    }
    ::system("pause");
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章