202 - 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.
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 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
题目大意就是说,计算循环小数,并找出循环节以及循环节的长度,最后输出时若循环节大于50最后输出省略号。做这题的时候求循环节判断结束写的好麻烦最后还有特色情况,后来看人家题解才知道这样求循环小数,记录被除数,若被除数相同了则证明循环了。最后就是循环小数可能并不是从第一位就开始循环啊,就这样WA了好几次。
//即使是循环小数,但可能并不是从第一位小数开始循环,WA了几次。。。
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int rm[1010];
int vis[1000010];
int rec[1000010];
int main()
{
int a,b;
while(~scanf("%d%d",&a,&b))
{
memset(vis,0,sizeof(vis));
memset(rec,0,sizeof(rec));
printf("%d/%d = %d",a,b,a/b);
a=a%b;
int k=1;
while(!vis[a]&&a)
{
vis[a]=k;
rec[k]=a;
a=a*10;
rm[k++]=a/b;
a=a%b;
}
printf(".");
for(int i=1;i<k&&i<=50;++i)
{
if(a&&a==rec[i])
printf("(");
printf("%d",rm[i]);
}
if(!a)
printf("(0");
if(k>50)
printf("...");
printf(")\n");
printf(" %d = number of digits in repeating cycle\n\n",a?(k-vis[a]):1);
}
return 0;
}