UVa202 Repeating Decimals

#include <stdio.h>
#include <map>
using namespace std;

int main()
{
    int a, b, c, q, r, places;
    map<int, int> rmap;
    pair<map<int, int>::iterator, bool> pr;
    int qarr[50];
    while (scanf("%d %d", &a, &b) == 2)
    {
        rmap.clear();
        places = 0;
        r = a % b;

        for (;;)    
        {
            c = r * 10;
            pr = rmap.insert(make_pair(c, places));
            if (!pr.second)
                break;
            q = c / b;
            r = c % b;
            if (places < 50)
                qarr[places] = q;
            ++places;
        }
        
        printf("%d/%d = %d.", a, b, a/b);
        r = places > 50 ? 50 : places;
        for (c = 0; c < r; ++c)
        {
            if (c == (pr.first)->second)
                putchar('(');
            printf("%d", qarr[c]);    
        }
        if (places > 50)
            printf("...");
        printf(")\n   %d = number of digits in repeating cycle\n\n", places - (pr.first)->second);
    }

    return 0;
}

posted on 2015-06-22 16:49  danny1221  阅读(346)  评论(0编辑  收藏  举报

导航