高精度的除法

版权声明:本文为博主原创文章,未经博主允许不得转载。Copyright © 钟波 https://blog.csdn.net/gzu_zb/article/details/89924468
//改代码用于精确计算除法的位数,比如求无限循环小数的循环节
//求循环节时,需要定义一个数组,用与标记是否有相同的余数,若是遇到时,结束循环,即得到循环节 
#include<iostream>
using namespace std;

int main() {
    int a, b;
    while (cin >> a >> b) {
        cout << a << "/" << b << "=";
        int cnt = a / b;
        cout << cnt;
        a -= cnt * b;
        a *= 10;
        if (a != 0) cout << ".";
        //        if(a==0) cout<<"0";
        int count = 0;
        while (a) {
            int ans = a / b;
            cout << ans;
            count++;
            if (count == 20) break;
            a -= ans * b;
            a *= 10;
        }
        cout << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/gzu_zb/article/details/89924468