循环小数(Repeating Decimals)

题目

The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number(fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have nosuch repeating cycles.

Examples of decimal expansions of rational numbers and their repeating cycles are shown below.Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.

 

Write a program that reads numerators and denominators of fractions and determines their repeatingcycles.

For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal lengthstring of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thusfor example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0which begins at positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).

 

Input

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

 

Output

For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycleto the right of the decimal or 50 decimal places (whichever comes first), and the length of the entirerepeating cycle.

In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If theentire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cyclebegins — it will begin within the first 50 places — and place ‘...)’ after the 50th digit.

 

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

 

题目解读

输入正整数a和b,输出a/b的循环小数表示及循环节长度。


分析:

  n除以m的余数只能是0~m-1,根据抽屉原则,当计算m+1次时至少存在一个余数相同,

            即为循环节;存储余数和除数,输出即可。

抽屉原则:

将m件物品按任何方式放入n(n<m)个抽屉,则必至少有一个抽屉里放有两件或两件以上的物品。

即当出现余数相同的情况时,即为循环节。

 

c实现

说明:r数组用来存储商,u数组用来标记为n的余数是否出现过以及出现的位置,

#include<stdio.h>
#include<string.h>

int r[3005],u[3005],s[3005];
int main()
{
    int n,m,t;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(r,0,sizeof(r));
        memset(u,0,sizeof(u));
        int count = 0;
        int t = n;
        //r存储的是商 
        r[count++]=n/m;
        //n是余数
        n = n%m;
        //如果余数没有出现过并且余数不为0;u[n]用来标记为n的余数是否出现过及出现的位置 
        while(!u[n]&&n){
            u[n] = count;
            s[count]=n;//s[count]用来记每个位置上对应的余数 
            r[count++] = 10*n/m;//每个位置上得到的商 
            n = 10*n%m; //得到余数 
        }
        //跳出循环后,相当于n开始出现重复,即循环的开始(循环开始位置的余数) 
        printf("%d/%d = %d",t,m,r[0]);
        printf(".");
        for(int i=1;i<count&&i<50;i++)
        {
            //对应位置为余数为n,即循环开始出现时 
            if(s[i]==n){
                printf("(");
            } 
            printf("%d",r[i]);
        }
        if(count>50) printf("...");
        if(!n) printf("(0");
        printf(")\n");
        //count-u[n]表示 小数点后循环结束时的位置减去循环开始的位置 
        printf(" %d = number of digits in repeating cycle",!n?1:count-u[n]);
    }
    return 0;
}
posted @ 2020-05-27 23:59  Vincent-yuan  阅读(669)  评论(0编辑  收藏  举报