Exercise 3_08 Repeating decimals (JAVA language)

package Chapter 3 exercises;
import java.util.Arrays;
import java.util.Scanner;
/*
 * Input integers a and b (0<=a<=3000, 1<=b<=3000),
 * output a/ The recurring decimal representation of b and the length of the recurring section.
For example, a=5, b=43, the
decimal representation is 0. (116279069767441860465), the length of the loop byte is 21
 */
//The loop does not necessarily start with one digit after the decimal point, it may start looping after the
public class exercise 3_08 Loop Decimal {
final static int MAX = 3005;
static int cx[]=new int[MAX];
static int fs[]=new int[MAX];
public static void main(String[] args) {
int n, m , t, i;
int cou= 0;
Scanner in=new Scanner(System.in);
n=in.nextInt();
m=in.nextInt();
cou = 0;
Arrays.sort(cx);
       Arrays. sort(fs);
   System.out.print(n+"/"+m+"="+n/m+".");
   t = n;
   while(true)
   {
       t = t % m * 10;
       fs[cou] = t / m;
       for(i = 0; i < cou; ++i)
       if(cx[i] == t) break;
       if(i != cou) break;
       cx[cou++] = t;
   }


   for(int q = 0; q < i;q++)
       System.out.print(fs[q]);
   System.out.print("(");


   if(cou > 50)
   {
       for(int q = i; q < i+50; q++)
           System.out.println(fs[q]);
       System.out.print("...");
   }
   else
   {
       for(int q = i;q < cou; q++)
           System.out.print(fs[q]);
   }
       System.out.println(")\n"+(cou-i)+"= number of digits in repeating cycle");
}


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325744687&siteId=291194637