202 Repeating Decimals
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.
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
求小数的循环节,我们知道,在模拟除法的时候,一位小数是由上一步得到的余数决定的,于是,只要同一个余数出现两次,循环节必定出现,但是余数与商是上一个与下一个的对应关系,为了方便,我设置了一个起始余数,就是最开始的分子对分母取余(分子小于等于分母),这样下一步求得的余数与商就有了对应关系,大家也可以采用别的方法处理,只要别搞晕了就行。
这里虽然限时是3秒,但是能优化还是要尽量优化,我就是使用了一个visit数组记录每个余数出现的位置,这样不仅在判断余数是否出现起了很好的加速作用,也方便了找到循环的起点。
#include <iostream> #include <stdio.h> #include <memory.h> #define MAX 50000 using namespace std; int Rem[MAX]; int Dec[MAX]; int dec_length; int rep_length; int visit[MAX]; int mol;//分子 int den;//分母 int rep_pos;//循环位置 void get_rep() { dec_length=0; int rem_temp=mol%den;//最开始的余数 int dec_temp; while(1) { mol=rem_temp;//被除数等于余数 mol*=10;//被除数扩增 dec_temp=mol/den;//计算新的商 Rem[dec_length]=rem_temp; Dec[dec_length]=dec_temp; if(rem_temp==0)//余数为0 { rep_pos=dec_length; rep_length=1; break; } if(visit[rem_temp]!=-1)//余数曾经出现过 { rep_pos=visit[rem_temp]; rep_length=dec_length-rep_pos; break; } else//余数不为0且从未出现过 { visit[rem_temp]=dec_length; dec_length++; } rem_temp=mol%den;//计算新的余数 } } void init() { for(int i=0;i<MAX;i++) visit[i]=-1; memset(Dec,0,sizeof(Dec)); memset(Rem,0,sizeof(Rem)); } int main() { int i,j; int rep_end; int cas=0; while(~scanf("%d%d",&mol,&den)) { init(); printf("%d/%d = %d.",mol,den,mol/den); mol%=den;//纯小数 get_rep(); rep_end=rep_pos+rep_length; for(i=0;i<rep_pos;i++)//输出不循环部分 { printf("%d",Dec[i]); } printf("("); for(i=rep_pos;i<(rep_end<50?rep_end:50);i++)//输出循环部分 { printf("%d",Dec[i]); } if(rep_end>50)//大于50输出省略号 printf("..."); printf(")\n"); printf(" %d = number of digits in repeating cycle\n",rep_length); printf("\n");//坑爹的空行,错了n此就是因为这个,题目没说,但是样例输出有 //printf("rep_pos=%d,rep_length=%d\n",rep_pos,rep_length); } return 0; }