码迷,mamicode.com
首页 > 其他好文 > 详细

UVa202 Repeating Decimals

时间:2015-06-22 17:38:27      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

UVa202 Repeating Decimals

标签:

原文地址:http://www.cnblogs.com/danny1221/p/4593488.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!