分数精确值

FisherKK edited 10 月前

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int N, m, n;
set<int> Si;
vector<int> Vi;
vector<int> Vmod;
void solve();
int main() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        cout << "case #" << i << ":\n";
        solve();
    }
}
void solve() {
    Si.clear(); Vi.clear(); Vmod.clear();
    cin >> m >> n; m *= 10;
    int cir;
    Si.insert(m / 10); Vmod.push_back(m / 10);
    bool flag = true;
    while (true) {
        int num = m / n, mod = m % n;
        Vi.push_back(num);
        if (Si.count(mod)) { 
            flag = false; cir = mod; 
            //if (num != Vi.back()) Vi.push_back(num);
            break;
        }
        else { Vmod.push_back(mod); Si.insert(mod);}
        if (!mod) break;
        m = mod * 10;
    }
    cout << "0.";
    if (flag) {
        for (int x : Vi) cout << x;
    } else {
        int j;
        for (int i = 0; i < Vi.size(); i++) {
            if (Vmod[i] == cir) { j = i;}
            cout << Vi[i];
        }
        cout << endl << j + 1 << '-' << Vi.size();
    }
    cout << endl;
}

Comments