算法竞赛入门经典java版程序CH3 UVA202

package ch3.uva202;

import java.util.*;

//UVa202
class Main {
	
	public static int[] shanArr=new int[3005];
	public static int[] yuArr=new int[3005];
	
	public static void main(String[] args) {
		int fz, fm;
		Scanner cin = new Scanner(System.in);		
		while (cin.hasNext()) {
			fz = cin.nextInt();
			int t=fz;
			fm= cin.nextInt();
			int yu=fz%fm;
			int count=0;
			int[] u=new int[3005];
			Arrays.fill(shanArr, 0);
			Arrays.fill(yuArr, 0);
			//System.out.println(yu+" "+u[yu]);
			while(fz>0&&u[yu]==0){	//u[yu]!=0表示yu重复了
				u[yu]=count;//记录每次余数对应的进入次数
				shanArr[count]=fz/fm;
				yu=fz%fm;
				yuArr[count++]=fz;						
				//System.out.printf("fz=%d,fm=%d,shan=%d yu=%d\n",fz,fm,fz/fm,fz%fm);
				fz=fz%fm*10;
				//cin.nextLine();
			}

			System.out.printf("%d/%d = %d.",t,fm,shanArr[0]);
			for(int i=1;i<count&&i<=50;i++){
				//System.out.println("in for: fz="+fz);
				if(yuArr[i]==fz &&fz!=0)
					System.out.printf("(");
				System.out.printf("%d", shanArr[i]);
			}
			if(fz==0){
				System.out.printf("(0");
			}
			if(count>50){
				System.out.printf("...");
			}
			System.out.printf(")\n");
			int newcount=count-u[yu];//总次-第一次出现我重复余的次数
			if(fz==0)
				newcount=1;//除尽了
			System.out.printf("   %d = number of digits in repeating cycle\n\n",newcount);
		}
	}
}

/*

Sample Input
76 25
5 43
1 397
1 6
5 7
1 250
300 31
655 990

Sample Output
76/25 = 3.04(0)
1 = number of digits in repeating cycle
5/43 = 0.(116279069767441860465)
21 = number of digits in repeating cycle
1/397 = 0.(00251889168765743073047858942065491183879093198992...)
99 = number of digits in repeating cycle

 */

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值