1697: Expanding Fractions 循环节

该问题要求输出两个正整数相除的十进制表示。当整数相除时,某些情况下会得到带有循环数字序列的十进制数。你需要找出这些循环序列,并在循环开始重复之前停止打印。如果存在循环序列,需要指出循环序列包含的数字数量。输入包括多个实例,每个实例由一行中的两个正整数组成,第一个是分子,第二个是分母。分子始终小于分母,且分母小于1000。输入以分子和分母都为零结束。输出应显示分数的十进制表示,如果表示终止,则打印完整的十进制表示;如果表示无限循环,则打印到循环模式首次重复之前的数字。注意,应找到最短的循环模式。每行输出的十进制数字(包括小数点)不应超过50个字符,最后一行除外。循环结束后,应显示'终止'或'最后n位数字永远重复',其中n是循环序列的数字数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1697: Expanding Fractions


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K19151Standard

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;
}

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值