循环小数(Repeating Decimals)

Repeating Decimals

Time limit: 3.000 seconds

Repeating Decimals 

The decimal expansion of the fraction 1/33 is tex2html_wrap_inline43 , wherethe tex2html_wrap_inline45 is used to indicate that the cycle 03 repeatsindefinitely with no intervening digits. In fact, the decimal expansion ofevery rational number (fraction) has arepeating cycle as opposed to decimal expansions of irrational numbers,which have no such repeating cycles.

Examples of decimal expansions of rational numbers and their repeating cyclesare shown below. Here, we useparentheses to enclose the repeating cycle rather than place a bar overthe cycle.

tabular23

Write a program that reads numerators and denominators of fractions anddetermines their repeating cycles.

Forthe purposes of this problem, define a repeating cycle of a fraction to bethe first minimal length string of digits tothe right of the decimal that repeats indefinitely with no intervening digits.Thus for example, the repeating cycle ofthe fraction 1/250 is 0, which begins at position 4 (as opposed to 0 whichbegins at positions 1 or 2 and as opposedto 00 which begins at positions 1 or 4).

Input

Each line of the input file consists of an integer numerator, which isnonnegative, followed by an integerdenominator, which is positive. None of the input integers exceeds 3000.End-of-file indicates the end of input.

Output

For each line of input, print the fraction, its decimal expansion through thefirst occurrence of the cycle to the rightof the decimal or 50 decimal places (whichever comes first), and the lengthof the entire repeating cycle.

In writingthe decimal expansion, enclose the repeating cycle in parentheses when possible.If the entire repeating cycle doesnot occur within the first 50 places, place a left parenthesis where thecycle begins - it will begin within the first 50places - and place ``...)" after the 50th digit.

Print a blank line after every test case.

Sample Input

76 25
5 43
1 397

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


题目:
输入整数a和b(0<=a<=3000, 1<=b<=3000),输出a/b的循环小数表示以及循环节长度。例如a=15,b=43,小数表示为0.(116279069767441860465),循环节长度为21。
【分析】

       当整数相除的运算过程中再次出现了同一个余数,说明循环节出现。(用小学的思想想想我们是如何进行除法运算的,如何去判断是否出现循环节)

用java语言编写程序,代码如下:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int a, b;
		while(input.hasNext()) {
			a = input.nextInt();
			b = input.nextInt();
			//b<=3000,也就是说循环小数位的长度 最高可多达3000位。
			//因此创建数组进行保存时,数组大小因大于3000,这里我可是吃了个大亏呀~~
			int[] num = new int[3005];//保存
			int[] digit = new int[3005];//保存每次运算后的余数
			
			int a1 = a;
			int tempD = 0;//临时的余数
			num[0] = a/b;
			tempD = a % b;
			a = a % b * 10;
			
			System.out.print(a1 + "/" + b + " = " + num[0] + ".");
			int k = 0;
			
			//当余数再次出现时,说明循环节出现
			while(a != 0 && digit[tempD] == 0) {
				num[++k] = a/b;
				digit[tempD] = k;
				tempD = a % b;
				a = a % b * 10;										
			}
			
			if(a == 0) {
				for(int i = 1; i <= k; i++)
					System.out.print(num[i]);
				System.out.println("(0)");
				System.out.println("   1 = number of digits in repeating cycle");
			}
			else {
				for(int i = 1; i < digit[tempD] ; i++) {
					System.out.print(num[i]);
				}
				
				//输出循环节
				System.out.print("(");
				for(int i = digit[tempD]; i <= k && i < digit[tempD] + 50; i++) {
					System.out.print(num[i]);
				}
				
				//判断循环节的长度
				if(k - digit[tempD] + 1 > 50)
					System.out.print("...");
				
				System.out.println(")");
				System.out.println("   " + (k - digit[tempD] + 1) + " = number of digits in repeating cycle");					
			}
			System.out.println();
		}
	}
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值