欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVA - 202 Repeating Decimals

程序员文章站 2024-03-19 08:51:04
...

UVA - 202 Repeating Decimals


#include"cstdio"
#include"string.h"
int th[3005];
int ans[3005];
int main()
{
    int a, b;
    while (scanf("%d%d", &a, &b) != EOF)
    {
        int fir = a / b;
        int x = (a % b) * 10;
        int i = 0;
        int len = 0;
        int bre = 0;
        while (1)
        {
            th[i] = x;//被除数
            ans[i] = x / b;//运算的结果
            int mod = x % b;//运算后的余数
            for (int j = 0; j < i; ++j)
            {
                if (th[i] == th[j])//找到一个相同的被除数,后面就和前面某个部分一样了,可以判断循环了
                {
                    bre = j;
                    len = i - j;
                    break;
                }
            }
            if (len)break;
            x = mod * 10; ++i;
        }
        printf("%d/%d = %d.", a, b, fir);
        for (int i = 0; i < bre; ++i)
            printf("%d",ans[i]);
        printf("(");
        if (len <= 50)
            for (int i = bre; i < bre + len; ++i)
                printf("%d", ans[i]);
        else for (int i = bre; i < bre + 50; ++i)
        {
            printf("%d", ans[i]);
            if (i == bre + 49)printf("...");
        }
        printf(")\n   %d = number of digits in repeating cycle\n\n", len);
    }
    return 0;
}