前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >循环小数(Repeating Decimals)

循环小数(Repeating Decimals)

作者头像
Vincent-yuan
发布2020-05-29 16:24:36
6130
发布2020-05-29 16:24:36
举报
文章被收录于专栏:Vincent-yuanVincent-yuan

题目

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的余数是否出现过以及出现的位置,

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档