首页 > 搜索 > BFS搜索 > POJ 1140 Expanding Fractions [解题报告] Java
2013
11-09

POJ 1140 Expanding Fractions [解题报告] Java

Expanding Fractions

问题描述 :

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.

输入:

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.

输出:

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.

Helpful hint: The number of digits before the pattern is repeated will never be more than the value of the denominator.

样例输入:

3 7
345 800
112 990
53 122
0 0

样例输出:

.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.

解题代码:

/* @author: */

import java.io.*;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner cin = new Scanner(new BufferedInputStream(System.in));
        int a,b,i,j,c[],f,d;
        c=new int[2000];
        for(;;)
        {
         a=cin.nextInt();
         b=cin.nextInt();
         if(a==0&b==0)break;
         c[0]=a;i=0;f=0;j=0;d=1;
         System.out.print(".");
         for(;;)
         {
          a=(a*10)%b;
          if(a!=0)
          {
           for(j=0;j< i;j++)
           {
            if(c[j]==c[i])break;
           }
           if(c[j]==c[i]&j!=i)break;
           System.out.printf("%d",c[i]*10/b);
           c[++i]=a;
           d++;
           if(d%50==0)System.out.println();
          }
          else
          {
           f=1;
           break;
          }
         }
         if(f==1)System.out.printf("%d\nThis expansion terminates.\n",c[i]*10/b);
         else
         {
          if(d%50!=0)System.out.printf("\n");
          System.out.printf("The last %d digits repeat forever.\n",i-j);
         }
        }
    }
}

  1. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.