UVA202 UVALive5141 Repeating Decimals【分数】

本文介绍了一种通过编程方式确定分数循环小数及其循环节长度的方法。文章详细阐述了如何通过模拟手工计算过程来找出循环节,并给出了完整的C语言程序实现。

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


World Finals >> 1990 - Washington


问题链接UVA202 UVALive5141 Repeating Decimals

问题简述

  输入两个整数numerator和denominator,分别为分子和分母。0≤分子,1≤分母≤3000。输出a/b的循环小数表示以及循环节长度。如果循环周期大于50,只显示50位,之后的全部用“...”表示

问题分析

  先取出整数部分(numerator/denominator的商),然后用余数numerator%denominator的余数)计算小数点后的各位。每次将余数先乘以10,就可以取出小数及其余数,循环往复。某两个位余数部分相同则表示,则表示这个区间小数点后的位形成循环

  该把该题归类为哪一类,有点困惑,也许归为模拟题是合适的,因为只要模拟人的计算过程就可以了。其实,这个题是一个数学题。

程序说明

  程序中,循环控制条件有点变态,但是是正确的。


AC的C语言程序如下:

/* UVA202 UVALive5141 Repeating Decimals */

#include <stdio.h>

#define MAXN 3000

int decimal[MAXN];
int numerator[MAXN];

int main(void)
{
    int n, d, start, end, i, j;

    while(scanf("%d%d", &n, &d) != EOF) {
        i = 0;
        numerator[i] = n % d;
        decimal[i] = numerator[i] * 10 / d;

        for(i=1; ;i++) {
            numerator[i] = numerator[i-1] * 10 % d;
            decimal[i] = numerator[i] * 10 / d;

            for(j=0; j<i; j++)
                if(numerator[j] == numerator[i])
                    break;
            if(j < i) {
                start = j;
                end = i - 1;
                break;
            }
            if(numerator[i] == 0) {
                start = i;
                end = i;
                break;
            }
        }

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

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值