【习题3-8】 UVA - 202 Repeating Decimals

来源:互联网 发布:淘宝卖家软件有哪些 编辑:程序博客网 时间:2024/05/23 17:33
Description

The decimal expansion of the fraction 1/33 is tex2html_wrap_inline43 , where the tex2html_wrap_inline45 is used to indicate that the cycle 03 repeats 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 no such 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.
tabular23
Write a program that reads numerators and denominators of fractions and determines their repeating cycles.
For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal length string of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thus for example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0 which 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 integer denominator, 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 the first occurrence of the cycle to the right of the decimal or 50 decimal places (whichever comes first), and the length of the entire repeating cycle.
In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle begins - it will begin within the first 50 places - 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



模拟小学除法,用数组存下商和余数,然后每除一次,看一下余数是否在前面出现过,如果出现过,那么从上一次这个相同余数的地方到这一次余数的地方之间为循环节。。

不过这题输出的要求到底是循环节内留50个数还是小数点后留50个数我也没读懂,毕竟英语渣难过,反正两种情况都交了一下都A了。。



AC代码:

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>using namespace std;int Find(const int *b, int n, int k){    int i;    for(i = 0; i < n; i++)        if(b[i] == k)            return i;    return -1;}int main(){    int n, m, t_n;//n除以m    int a[10000];//商    int b[10000];//余数    int i, j, flag, cycle;    while(cin >> n >> m)    {        memset(a, 0, sizeof(a));        memset(b, 0, sizeof(b));        i = 0;        t_n = n;        do        {            a[i] = n / m;            b[i] = n % m;            n = b[i] * 10;            i++;            flag = Find(b, i - 1, b[i - 1]);        }        while(flag == -1);        cycle = i - flag - 1;        printf("%d/%d = ", t_n, m);        for(j = 0; j < i; j++)        {            if(j == 0)                printf("%d.", a[j]);            else                printf("%d", a[j]);            if(j == flag)                printf("(");            if(j - flag == 50)            {                printf("...");                break;            }        }        printf(")\n   %d = number of digits in repeating cycle\n\n", cycle);    }    return 0;}


0 0
原创粉丝点击