1697: Expanding Fractions
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 3s | 8192K | 191 | 51 | Standard |
In this problem you are to print the decimal expansion of a quotient of two integers. As you well know, the decimal expansions of many integer quotients result in decimal expansions with repeating sequences of digits. You must identify these. You will print the decimal expansion of the integer quotient given, stopping just as the expansion terminates or just as the repeating pattern is to repeat itself for the first time. If there is a repeating pattern, you will say how many of the digits are in the repeating pattern.
Input
There will be multiple input instances, each instance consists of two positive integers on a line. The first integer represents the numerator of the fraction and the second represents the denominator. In this problem, the numerator will always be less than the denominator and the denominator will be less than 1000. Input terminates when numerator and denominator are both zero.
Output
For each input instance, the output should consist of the decimal expansion of the fraction, starting with the decimal point. If the expansion terminates, you should print the complete decimal expansion. If the expansion is infinite, you should print the decimal expansion up to, but not including the digit where the repeated pattern first repeats itself.
For instance, 4/11 = .3636363636..., should be printed as .36. (Note that the shortest repeating pattern should be found. In the above example, 3636 and 363636, among others, are repeating patterns, but the shortest repeating pattern is 36.)
Since some of these expansions may be quite long, multiple line expansions should each contain exactly 50 characters on each line (except the last line, which, of course, may be shorter) - that includes the beginning decimal point.
On the line immediately following the last line of the decimal expansion there should be a line saying either ``This expansion terminates.", or ``The last n digits repeat forever.", where n is the number of digits in the repeating pattern.
Output for each input instance (including the last input instance) should be followed by a blank line.
Helpful hint: The number of digits before the pattern is repeated will never be more than the value of the denominator.
Sample Input
3 7 345 800 112 990 53 122 0 0
Sample Output
.428571 The last 6 digits repeat forever. .43125 This expansion terminates. .113 The last 2 digits repeat forever. .4344262295081967213114754098360655737704918032786 885245901639 The last 60 digits repeat forever.
#include<stdio.h>
int main()
{
int a,b;
int i,j,t;
int f[1000],r[1000];
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==0&&b==0) break;
j=0;
r[0]=a;
for(i=0;;i++)
{
f[i]=a*10/b;
r[i+1]=a*10%b;
a=r[i+1];
if(a==0) break;
for(j=0;j<i+1;j++)
if(r[j]==r[i+1]) break;
if(j<i+1) break;
}
printf(".");
int count=1;
for(t=0;t<i+1;t++)
{
printf("%d",f[t]);
count++;
if(count%50==0&&t!=i)printf("/n");
}
printf("/n");
if(a==0) {printf("This expansion terminates./n");}
else
{ if(j==0) printf("The last %d digits repeat forever./n",i+1);
else printf("The last %d digits repeat forever./n",i+1-j);
}
printf("/n");
}
return 0;
}
该问题要求输出两个正整数相除的十进制表示。当整数相除时,某些情况下会得到带有循环数字序列的十进制数。你需要找出这些循环序列,并在循环开始重复之前停止打印。如果存在循环序列,需要指出循环序列包含的数字数量。输入包括多个实例,每个实例由一行中的两个正整数组成,第一个是分子,第二个是分母。分子始终小于分母,且分母小于1000。输入以分子和分母都为零结束。输出应显示分数的十进制表示,如果表示终止,则打印完整的十进制表示;如果表示无限循环,则打印到循环模式首次重复之前的数字。注意,应找到最短的循环模式。每行输出的十进制数字(包括小数点)不应超过50个字符,最后一行除外。循环结束后,应显示'终止'或'最后n位数字永远重复',其中n是循环序列的数字数量。
1102

被折叠的 条评论
为什么被折叠?



