码迷,mamicode.com
首页 > 编程语言 > 详细

[算法竞赛入门经典]Repeating Decimals, ACM/ICPC World Finals 1990,UVa202

时间:2018-09-22 14:40:10      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:nes   ems   begin   esc   eating   when   pac   小数   through   

Description

The decimal expansion of the fraction 1/33 is 0.03, where the 03 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.
fraction??decimal expansion??repeating cycle??cycle length
1/6????0.1(6)????6????1
5/7????0.(714285)????714285????6
1/250????0.004(0)????0????1
300/31????9.(677419354838709)????677419354838709????15
655/990????0.6(61)????61????2
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.

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

此题本人实在没什么好思路,看了某大佬的解题,发现绕不出他那种思路了....

CODE

#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#define MAX 3005

using namespace std;

int rem[MAX];       // 记录每次计算的余数
int res[MAX];       // 记录商
int cycle[MAX];     // 记录余数是否出现过(判断循环节的依据)

int main()
{
    int m,n;
    while(~scanf("%d%d",&m,&n)&&n){         // 分母为0退出循环
        memset(rem,0,sizeof(rem)); memset(res,0,sizeof(res)); memset(cycle,0,sizeof(cycle));
        int tmp = m;                        // tmp代替m参与模拟除法运算
        int res_cnt = 0;
        //TODO int decimal_cnt = 0;
        res[res_cnt++] = tmp / n;
        tmp = tmp % n;
        while(!cycle[tmp] && tmp){          // 余数重复或余数为0跳出(重复代表小数开始循环周期,0代表不是循环小数)
            cycle[tmp] = 1;                 // 表示余数tmp出现过
            rem[res_cnt] = tmp;             // 记录每一次相除的余数
            res[res_cnt++] = tmp * 10 / n;   
            tmp = (tmp * 10) % n;
        }
        printf("%d/%d = ",m,n);
        printf("%d.",res[0]);
        int index;
        for(int i = 1;i < res_cnt && i <= 50;i ++){
            if(rem[i]==tmp && tmp != 0) {   //从第一个余数开始往后数,如果此余数等于最后计算的余数,说明循环从此位置开始
                printf("("); index = i;
            }
            printf("%d",res[i]);
        }
        if(res_cnt> 50) printf("...");
        if(!tmp) { printf("(0"); index = 1;} 
        else index = res_cnt - index;
        printf(")\n");
        printf("%4d = number of digits in repeating cycle\n",index);
        
    }
}

[算法竞赛入门经典]Repeating Decimals, ACM/ICPC World Finals 1990,UVa202

标签:nes   ems   begin   esc   eating   when   pac   小数   through   

原文地址:https://www.cnblogs.com/1Kasshole/p/9689733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!