The decimalexpansion of the fraction 1/33 is ,where the isused to indicate that the cycle 03 repeats indefinitely with no interveningdigits. In fact, the decimal expansion of every rational number (fraction) hasa repeating cycle as opposed to decimal expansions of irrational numbers, whichhave no such repeating cycles.
Examples ofdecimal expansions of rational numbers and their repeating cycles are shownbelow. Here, we use parentheses to enclose the repeating cycle rather thanplace a bar over the cycle.
Write a programthat reads numerators and denominators of fractions and determines theirrepeating cycles.
For the purposesof this problem, define a repeating cycle of a fraction to be the first minimallength string of digits to the right of the decimal that repeats indefinitelywith no intervening digits. Thus for example, the repeating cycle of thefraction 1/250 is 0, which begins at position 4 (as opposed to 0 which beginsat positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).
Each line of theinput file consists of an integer numerator, which is nonnegative, followed byan integer denominator, which is positive. None of the input integers exceeds3000. End-of-file indicates the end of input.
For each line ofinput, print the fraction, its decimal expansion through the first occurrenceof the cycle to the right of the decimal or 50 decimal places (whichever comesfirst), and the length of the entire repeating cycle.
In writing thedecimal expansion, enclose the repeating cycle in parentheses when possible. Ifthe entire repeating cycle does not occur within the first 50 places, place aleft parenthesis where the cycle begins - it will begin within the first 50places - and place ``...)" after the50th digit.
Print a blank lineafter every test case.
76 25
5 43
1 397
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<iostream>
using namespace std;
const int N=2000;
int result[N];//存取结果
int mod[N];//存取余数
int main()
{
int a,b;
while(cin>>a>>b)
{
int length;
result[0]=a/b;
mod[0]=a%b;
int i,j;
for( i=1;i<N;i++)
{
int flag=0;
result[i]=mod[i-1]*10/b;
mod[i]=mod[i-1]*10%b;
for( j=0;j<i;j++)
{
if(mod[j]==mod[i])
{
length=i-j;
flag=1;
break;
}
}
if(flag==1)
{
break;
}
}
cout<<a<<"/"<<b<<" ="<<result[0]<<".";
for(int k=1;k<j+1;k++)
{
cout<<result[k];
}
cout<<"(";
if(i<=50)
{
for(int k=j+1;k<=i;k++)
{
cout<<result[k];
}
cout<<")\n";
}
else
{
for(int k=j+1;k<=50;k++)
{
cout<<result[k];
}
cout<<"...)\n";
}
cout<<" "<<length<<" = numberof digits in repeating cycle\n\n";
}
}
这题需要模拟除法,当出现相同的余数时,就代表循环点的出现。下面我以5/7为例详细的讲下思路。
如上图,第0步,分子是5,分母是7(分母在整个除法中始终不变),5/7的商是0,代表这个除法的整数部分是0,,余数是5,乘以10,得50作为第1步的分子;50/7的商为7,代表这个除法的第一位小数是7,余数是1,乘以10,得10作为第2步的分子...可以发现每步的除法只跟分子有关,也就是当分子出现重复值,如上图第1步和第7步的分子值都为50时,第7步后面的运算其实是在重复第1步至第6步间的运算。所以第1步至第7步就是一个循环节。
程序实现中,可以不用存储余数的值,只需存储上图中的分子和商,我定义了A[2][3010](注意循环长度一定不会大于分母,也就是3000次内一定会出现循环),用A[0][j]存储第j步的分子,A[1][j]存储第j步的商。注意最后的数值由商可以直接构成,如上图的红色字体,数值为0.7142857142...