zoukankan      html  css  js  c++  java
  • 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(")    %d = number of digits in repeating cycle ", places - (pr.first)->second);
        }

        return 0;
    }

  • 相关阅读:
    3-4: 一元多项式的乘法与加法运算
    设计模式一装饰者模式
    设计模式一组合模式
    设计模式一命令模式
    设计模式一建造者模式
    设计模式一桥接模式
    设计模式一适配器模式
    设计模式一抽象工厂模式
    排序算法一二分排序
    排序算法一希尔排序
  • 原文地址:https://www.cnblogs.com/danny1221/p/4593488.html
Copyright © 2011-2022 走看看