K - 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

思路:循环小数的除数肯定是循环出现的,用一个数组储存除数,当除数再次出现时,停止循环.

#include<stdio.h>
#include<string.h>
int main(void)
{
 int n,m;
 int a[30005];//储存各位小数
 int b[30005];//储存除数出现的次数
 int c[30005];//某个除数刚出现时对应小数的位置
 while(scanf("%d%d",&n,&m)!=EOF)
 {
     int sub=1;
     int p=n/m;
     printf("%d/%d = %d.",n,m,p);
     memset(b,0,sizeof(b));
     memset(c,0,sizeof(c));
     n=n%m*10;       //下一个被除数
     while(b[n]==0&&n){  //一旦被除数出现俩次  跳出循环
         b[n]=1;
         c[n]=sub;    //记录小数的位置
         a[sub]=n/m;   //记录小数
         sub++;
         n=n%m*10;
     }
     if(n==0){
         for(int i=1;i<sub;i++)printf("%d",a[i]);
         printf("(0)\n");
     }
     else {
         for(int i=1;i<c[n];i++)printf("%d",a[i]);
         printf("(");
         for(int i=c[n];i<sub&&i<=50;i++){
             printf("%d",a[i]);
         }
         if(sub>50)printf("...");
         printf(")\n");
     }
     if(n==0)printf("   1");
     else printf("   %d",sub-c[n]);
     printf(" = number of digits in repeating cycle\n\n");
 }
 return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43328040/article/details/88368116