习题3_08循环小数(JAVA语言)

package 第三章习题;
import java.util.Arrays;
import java.util.Scanner;
/*
 * 输入整数a和b(0<=a<=3000,1<=b<=3000),
 * 输出a/b的循环小数表示以及循环节长度。
例如,a=5,b=43,
小数表示为0.(116279069767441860465),循环字节长度为21
 */
//循环不一定是从小数点后一位数开始,可能从之后才开始循环
public class 习题3_08循环小数 {
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");
}


}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务