UVa202 Repeating Decimals

Input
Each line of the input le 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- le indicates the end
of input.
Output
For each line of input, print the fraction, its decimal expansion through the rst occurrence of the cycle
to the right of the decimal or 50 decimal places (whichever comes rst), 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 rst 50 places, place a left parenthesis where the cycle
begins | it will begin within the rst 50 places | and place `
...)
' after the 50th digit.
SampleInput
76 25
5 43
1 397
SampleOutput
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>
#include <cstdlib>
#include <string.h>
#include <cstdio>

int s[3001];
int m[3001];

int main()
{
    int a, b;
    int temp;
    while(scanf("%d %d", &a, &b) == 2)
    {
    	temp = a;
    	int flag = 1;
    	int count = 0;
    	int num = 1;
    	s[0] = a / b;  //整数数列 
    	m[0] = a % b;  //余数数列 
    	printf("%d/%d = ", a, b);
    	printf("%d.", s[0]);
    	if(m[0] != 0)
    	{	
			for(int i = 0; flag == 1; i++)
			{
				a = m[i];
				a = a * 10;
				s[i+1] = a / b;
				m[i+1] = a % b;
				for(int j = 0; j < i+1; j++)
				{
					if(m[i+1] == m[j])
					{
						count = j+1;
						flag = 0;
						break;
					}
				}
				num++;
			}
			for(int i = 0,j = 0; i < num-1; i++, j++)
			{
				if(j > 49 && i < num - 1)
				{
					printf("...");
					break;
				}
				if(i == count-1)
				  printf("(");
				printf("%d", s[i+1]);
			}
			printf(")\n");
			printf("   %d = number of digits in repeating cycle\n\n", num - count);
		}
		else
		{
			printf("(%d)\n", 0);
			printf("   %d = number of digits in repeating cycle\n\n", 1);
		}		
	}	
	return 0;	
} 




你可能感兴趣的:(uva)