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.
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
World Finals >> 1990 - Washington
问题链接:UVA202 UVALive5141 Repeating Decimals。
问题简述:
输入两个整数numerator和denominator,分别为分子和分母。0≤分子,1≤分母≤3000。输出a/b的循环小数表示以及循环节长度。如果循环周期大于50,只显示50位,之后的全部用“...”表示。
问题分析:
先取出整数部分(numerator/denominator的商),然后用余数(numerator%denominator的余数)计算小数点后的各位。每次将余数先乘以10,就可以取出小数及其余数,循环往复。某两个位余数部分相同则表示,则表示这个区间小数点后的位形成循环。
该把该题归类为哪一类,有点困惑,也许归为模拟题是合适的,因为只要模拟人的计算过程就可以了。其实,这个题是一个数学题。
程序说明:
程序中,循环控制条件有点变态,但是是正确的。
AC的C语言程序如下:
/* UVA202 UVALive5141 Repeating Decimals */
#include <stdio.h>
#define MAXN 3000
int decimal[MAXN];
int numerator[MAXN];
int main(void)
{
int n, d, start, end, i, j;
while(scanf("%d%d", &n, &d) != EOF) {
i = 0;
numerator[i] = n % d;
decimal[i] = numerator[i] * 10 / d;
for(i=1; ;i++) {
numerator[i] = numerator[i-1] * 10 % d;
decimal[i] = numerator[i] * 10 / d;
for(j=0; j<i; j++)
if(numerator[j] == numerator[i])
break;
if(j < i) {
start = j;
end = i - 1;
break;
}
if(numerator[i] == 0) {
start = i;
end = i;
break;
}
}
printf("%d/%d = %d.", n, d, n / d);
for(i=0; i<start; i++)
printf("%d", decimal[i]);
printf("(");
if(end - start + 1 > 50) {
for(i=start; i<start+50; i++)
printf("%d", decimal[i]);
printf("...");
} else {
for(i=start; i<=end; i++)
printf("%d", decimal[i]);
}
printf(")\n");
printf(" %d = number of digits in repeating cycle\n\n", end - start + 1);
}
return 0;
}