UVA202 - Repeating Decimals(循环小数循环节)

UVA202 - Repeating Decimals(循环小数循环节)

The decimal expansionof the fraction 1/33 is 0.03, where the 03 is used to indicatethat the cycle 03 repeats indefinitely with no intervening digits. In fact, the decimalexpansion of every rationalnumber (fraction) has a repeating cycle as opposedto decimal expansions of irrational numbers, which have no such repeating cycles.

Examples of decimalexpansions of rationalnumbers and theirrepeating cycles are shown below.

Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.

 

fraction 1/6

decimal expansion 0.1(6)

repeating cycle 6

cycle length 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 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 firstminimal length string of digits to the right of the decimal that repeats indefinitely with no intervening digits.Thus for example,the repeating cycleof the fraction 1/250 is 0, which begins at position 4 (as opposedto 0 which begins at positions1 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 exceeds3000. End-of-file indicates the end of input.

 

Output

For each line of input, print the fraction, its decimal expansion through the firstoccurrence 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 writingthe decimal expansion, enclose the repeatingcycle in parentheses when possible. If the entire repeating cycle does not occur within the first50 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 digitsin repeating cycle

 

5/43 = 0.(116279069767441860465)

21 = numberof digits in repeating cycle

 

1/397 = 0.(00251889168765743073047858942065491183879093198992...)

99 = numberof digits in repeating cycle


题意:

3000以内的两个数字相除,输出小数结果,若为循环小数,使用(括起最短循环节)

思路:

当存在余数相同的位置时,既是有循环节。

#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 2010
#define M 100007
int r[3005],u[3005],s[3005];
using namespace std;
int main(){
	int n,t,m;
	while(~scanf("%d%d",&n,&m)){
		memset(r,0,sizeof(r));
		memset(u,0,sizeof(u));
		int count=0;
		int t=n;
		r[count++]=n/m;
		//cout<<"n/m="<50){
				printf("...");
			}
			if(!n){
				printf("(0");
			}
			printf(")\n");
			printf("   %d = numberof digits in repeating cycle\n\n",!n?1:count-u[n]);
		
	}
	return 0;
}
结果分析:
5 43
n/m=0
n%m=5
u[n]=1
s[count]=5
r[count++]=1
n=10*n%m--7
count=2
u[n]=2
s[count]=7
r[count++]=1
n=10*n%m--27
count=3
u[n]=3
s[count]=27
r[count++]=6
n=10*n%m--12
count=4
u[n]=4
s[count]=12
r[count++]=2
n=10*n%m--34
count=5
u[n]=5
s[count]=34
r[count++]=7
n=10*n%m--39
count=6
u[n]=6
s[count]=39
r[count++]=9
n=10*n%m--3
count=7
u[n]=7
s[count]=3
r[count++]=0
n=10*n%m--30
count=8
u[n]=8
s[count]=30
r[count++]=6
n=10*n%m--42
count=9
u[n]=9
s[count]=42
r[count++]=9
n=10*n%m--33
count=10
u[n]=10
s[count]=33
r[count++]=7
n=10*n%m--29
count=11
u[n]=11
s[count]=29
r[count++]=6
n=10*n%m--32
count=12
u[n]=12
s[count]=32
r[count++]=7
n=10*n%m--19
count=13
u[n]=13
s[count]=19
r[count++]=4
n=10*n%m--18
count=14
u[n]=14
s[count]=18
r[count++]=4
n=10*n%m--8
count=15
u[n]=15
s[count]=8
r[count++]=1
n=10*n%m--37
count=16
u[n]=16
s[count]=37
r[count++]=8
n=10*n%m--26
count=17
u[n]=17
s[count]=26
r[count++]=6
n=10*n%m--2
count=18
u[n]=18
s[count]=2
r[count++]=0
n=10*n%m--20
count=19
u[n]=19
s[count]=20
r[count++]=4
n=10*n%m--28
count=20
u[n]=20
s[count]=28
r[count++]=6
n=10*n%m--22
count=21
u[n]=21
s[count]=22
r[count++]=5
n=10*n%m--5
count=22
5/43 = 0.(116279069767441860465)
   21 = numberof digits in repeating cycle



你可能感兴趣的:(数学)