UVA 202 循环点


 Repeating Decimals 

The decimal expansion of the fraction 1/33 is tex2html_wrap_inline43 , wherethe tex2html_wrap_inline45 is used to indicate that the cycle 03 repeatsindefinitely with no intervening digits. In fact, the decimal expansion ofevery rational number (fraction) has arepeating cycle as opposed to decimal expansions of irrational numbers,which have no such repeating cycles.

Examples of decimal expansions of rational numbers and their repeating cyclesare shown below. Here, we useparentheses to enclose the repeating cycle rather than place a bar overthe cycle.

tabular23

Write a program that reads numerators and denominators of fractions anddetermines their repeating cycles.

Forthe purposes of this problem, define a repeating cycle of a fraction to bethe first minimal length string of digits tothe right of the decimal that repeats indefinitely with no intervening digits.Thus for example, the repeating cycle ofthe fraction 1/250 is 0, which begins at position 4 (as opposed to 0 whichbegins at positions 1 or 2 and as opposedto 00 which begins at positions 1 or 4).

Input

Each line of the input file consists of an integer numerator, which isnonnegative, followed by an integerdenominator, which is positive. None of the input integers exceeds 3000.End-of-file indicates the end of input.

Output

For each line of input, print the fraction, its decimal expansion through thefirst occurrence of the cycle to the rightof the decimal or 50 decimal places (whichever comes first), and the lengthof the entire repeating cycle.

In writingthe decimal expansion, enclose the repeating cycle in parentheses when possible.If the entire repeating cycle doesnot occur within the first 50 places, place a left parenthesis where thecycle begins - it will begin within the first 50places - and place ``...)" after the 50th digit.

Print a blank line after every test case.

Sample Input

76 25
5 43
1 397

Sample Output

76/25 = 3.04(0)
   1 = number of digits in repeating cycle

5/43 = 0.(116279069767441860465)
   21 = number of digits in repeating cycle

1/397 = 0.(00251889168765743073047858942065491183879093198992...)
   99 = number of digits in repeating cycle


//第一,查找循环点,如果被除数是m的话,那么他的余数只能是0~m-1。所以通过查找商和余数就可以找到循环点
//拿5/43举个例子
//商:    0  1  1  6  2  7  9  0  6  9  7  6  7  4  4  1  8  6  0  4  6  5  1 
//余数:  5  7  27 12 34 39 3  30 42 33 29 32 19 18 8 37 26  2  20 28 22 5  7  
//其中第一组为小数点前边的值,最后一组跟前边是重合,那么就表明循环点出现了
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;
#define maxn 3005
int r[maxn]; //余数
int s[maxn]; //商
int judge[maxn];

int main()
{
    int a,b;
    while(scanf("%d %d",&a,&b))
    {
        int m=a;
        int n=b;
        int count=0;
        memset(r,0,sizeof(r));
        memset(s,0,sizeof(s));
        memset(judge,0,sizeof(judge));
        s[count++]=a/b;
        a=a%b;
        while(a!=0 && judge[a]==0) //n不为0  或者没有找到循环节点
        {
            judge[a]=1;
            s[count] = a*10/b;
            r[count] = a*10%b;
            //printf("%d %d\n",s[count],r[count]);
            count++;
            a=a*10%b;
            //printf("a=%d,judge[a]=%d\n",a,judge[a]);
        }
       // printf("%d\n",count);
        printf("%d/%d = ",m,n);
        if(a == 0)
        {
            printf("%d.",s[0]);
            for(int i=1;i<count;++i)
                printf("%d",s[i]);
            printf("(0)\n   1 = number of digits in repeating cycle\n\n");
        }
        else
        {
            if(count<=51)
            {
                printf("%d.",s[0]);
                for(int i=1;i<count;++i)
                {
                    if(i==1)
                        printf("(");

                    printf("%d",s[i]);
                    if(i==count-1)
                        printf(")\n");
                }
                printf("   %d = number of digits in repeating cycle\n\n",count-1);
            }
            else
            {
                printf("%d.",s[0]);
                for(int i=1;i<=50;++i)
                {
                    if(i==1)
                        printf("(");

                    printf("%d",s[i]);
                    if(i==50)
                        printf("...)\n");
                }
                printf("   %d = number of digits in repeating cycle\n\n",count-1);
            }
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值