循环小数(Repeating Decimals)

2023-05-16

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();
		}
	}
}



本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

循环小数(Repeating Decimals) 的相关文章

  • 数组旋转新方法

    题目 xff1a 对一个int数组进行左右任意长度的旋转 xff0c 如 xff1a 原始数组为 1 2 3 4 5 xff0c 左旋两位 xff08 可用 2表示 xff09 得 3 4 5 1 2 xff0c 右旋两位 xff08 可用
  • 常见Linux shell脚本中的“-e -d -f -eq -ne -gt -ge”操作符的含义

    常见shell脚本中的 e d f eq ne gt ge 操作符的含义 xff1a 文件表达式 e filename xff1a 如果filename存在 xff0c 则为真 d filename xff1a 如果filename为目录
  • 基于注解的spring源码解析之总体流程

    基于注解的spring源码解析1 总体流程 总体流程图 Demo代码 span class token keyword public span span class token keyword class span span class t
  • Android ViewBinding 替换 findViewById 的神器

    ViewBinding中文官网 ViewBinding 的出现就是为了替代 findViewById 的 以前我们写完布局后就要在代码中使用 findViewById 方法找到 xml 文件中对应的 view xff0c 这样耗时费力 xf
  • 套接字选项(SO_RCVBUF和SO_SNDBUF)

    有时候我们需要控制套接字的行为 如修改缓冲区的大小 这个时候我们就要学习套接字选项 int getsockopt int sockfd int level int optname void optval socklen t optlen i
  • 欢迎使用CSDN-markdown编辑器

    欢迎使用Markdown编辑器写博客 本Markdown编辑器使用StackEdit修改而来 xff0c 用它写博客 xff0c 将会带来全新的体验哦 xff1a Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传
  • 工作一年,辞职复习半年,考杭电计算机的经验分享

    工作一年 xff0c 辞职复习半年 xff0c 考杭电计算机的经验分享 如果 xff0c 毕业了工作顺利的人大概率是不会去考研的 xff0c 去考研的人 xff0c 大概率是想改变的 题记 2019 4 6 关于我 纠结的人生 为什么考研
  • ICPR-2018-OCR笔记

    2018年第24届国际模式识别大会International Conference on Pattern Recognition ICPR 在北京国家会议中心召开 xff0c 会议从8月20日到24日持续1周时间 有阿里的读光平台的介绍 x
  • centos7 firewalld导致docker网络异常

    centos7 自带防火墙是firewalld 在某下情况下可能导致docker 的某些网络问题 docker 有4种网络模式 xff1a 1 bridge模式 xff08 默认 xff09 xff1a 网桥模式 xff0c 通过虚拟网桥使
  • Linux(centOS)安装yum

    查看是否有安装yum rpm qa grep yum 红框内代表已安装的 xff0c 如果为空代表未安装yum 删除yum下的所有组件 rpm qa grep yum xargs rpm e nodeps 查看原有的yum配置 xff0c
  • Java实现-删除数字

    给出一个字符串 A 表示一个 n 位正整数 删除其中 k 位数字 使得剩余的数字仍然按照原来的顺序排列产生一个新的正整数 找到删除 k 个数字之后的最小正整数 N lt 61 240 k lt 61 N 您在真实的面试中是否遇到过这个题 x
  • CentOS7, CentOS8 firewalld docker 端口映射问题,firewall开放端口后,还是不能访问,解决方案

    启动sqlserver容器 docker run d restart 61 always e 34 ACCEPT EULA 61 Y 34 e 34 SA PASSWORD 61 Abc12345 34 p 1433 1433 name s
  • firewalld 命令大全

    1 firewalld的基本使用 启动 xff1a systemctl start firewalld 查看状态 xff1a systemctl status firewalld 停止 xff1a systemctl disable fir
  • vscode通过文件名查找文件的方法

    vscode通过文件名查找文件的方法 这篇文章给大家分享的是有关vscode通过文件名查找文件的方法的内容 小编觉得挺实用的 xff0c 因此分享给大家做个参考 一起跟随小编过来看看吧 按快捷键ctrl 43 p可以弹出一个小窗 xff0c
  • HTML几种设置水平居中和垂直居中的方式

    水平居中 1 平居中设置 定宽块状元素 当被设置元素为 块状元素 时用 text align xff1a center 就不起作用了 xff0c 这时也分两种情况 xff1a 定宽块状元素和不定宽块状元素 这一小节我们先来讲一讲定宽块状元素
  • 【Django网络安全】如何正确设置跨域

    原文作者 xff1a 我辈李想 版权声明 xff1a 文章原创 xff0c 转载时请务必加上原文超链接 作者信息和本声明 Django网络安全 Django网络安全 如何正确设置跨域 Django网络安全 如何正确防护CSRF跨站点请求伪造
  • 我怎么能只懂得使用Redis呢?(一)

    前言 最近在回顾 amp 学习Redis相关的知识 xff0c 也是本着分享的态度和知识点的记录在这里写下相关的文章 希望能帮助各位读者学习或者回顾到Redis相关的知识 xff0c 当然 xff0c 本人才疏学浅 xff0c 在学习和写作
  • 统计回文子串

    1054 统计回文子串 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 字符串处理回文串 题目描述 现在给你一个字符串S xff0c 请你计算S中有多少连续子串是回文串 输入格式 输入
  • 小明养猪的故事

    1064 小明养猪的故事 分数 1 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 递推 题目描述 话说现在猪肉价格这么贵 xff0c 小明也开始了养猪生活 说来也奇怪 xff0c 他养的猪一出
  • 找新朋友

    1079 找新朋友 分数 1 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 简单数学题公约数 题目描述 新年快到了 xff0c 天勤准备搞一个聚会 xff0c 已经知道现有会员N人 xff0c

随机推荐

  • 生物节律

    1084 生物节律 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 数学题 题目描述 有些人相信 xff0c 人从出生开始就有三个生物周期 这三个生物周期分别是体力 xff0c 情绪和
  • 鸡兔同笼

    鸡兔同笼 已知鸡和兔的总数量为n xff0c 总腿数为m 输入n和m xff0c 依次输出鸡的数目和兔的数目 如果无解 xff0c 则输出No answer 样例输入 xff1a 14 32 样例输出 xff1a 12 2 样例输入 xff
  • 赌徒

    1097 赌徒 分数 2 时间限制 xff1a 1 秒 内存限制 xff1a 32 兆 特殊判题 xff1a 否 标签 查找二分查找 题目描述 有n个赌徒打算赌一局 规则是 xff1a 每人下一个赌注 xff0c 赌注为非负整数 xff0c
  • 排列(permutation)

    排列 xff08 permutation xff09 用1 2 3 xff0c xff0c 9组成3个三位数 abc xff0c def 和 ghi xff0c 每个数字恰好使用一次 xff0c 要求 abc def ghi 61 1 2
  • SQL Server Management Studio 使用方法手记

    本方法只记录自己的操作方法 xff0c 仅供参考 一 下载安装 SQL Server Management Studio V15 0 18424 0 xff0c 链接 xff1a https download csdn net downlo
  • 蛇形填数(矩阵)

    蛇形填数 在 n x n 方针里填入 1 2 xff0c xff0c n x n xff0c 要求填成蛇形 例如 xff1a n 61 4时方阵为 xff1a 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 上
  • WERTYU

    Problem 1343 WERTYU Time Limit 1000 mSec Memory Limit 32768 KB Problem Description A common typing error is to place the
  • 分子量(Molar Mass)

    Molar mass Time limit 3 000 seconds An organic compound is any member of a large class of chemical compounds whose molec
  • 数数字(Digit Counting)

    Digit Counting Time limit 3 000 seconds Trung is bored with his mathematics homeworks He takes a piece of chalk and star
  • 周期串(Periodic Strings)

    周期串 Periodic Strings 如果一个字符串可以由某个长度为k的字符串重复多次得到 xff0c 则称该串以k为周期 例如 xff0c abcabcabcabc以3为周期 xff08 注意 xff0c 它也以6和12为周期 xff
  • 谜题(Puzzle)

    Puzzle Time limit 3 000 seconds Puzzle A children 39 s puzzle that was popular 30 years ago consisted of a 5x5 framewhic
  • 纵横字谜的答案(Crossword Answers)

    Crossword Answers Time limit 3 000 seconds Crossword Answers A crossword puzzle consists of a rectangular grid of black
  • DNA序列(DNA Consensus String)

    DNA Consensus String Time limit 3 000 seconds Figure 1 DNA Deoxyribonucleic Acid is the molecule which contains the gene
  • 古老的密码(Ancient Cipher)

    Ancient Cipher Time limit 3 000 seconds Ancient Roman empire had a strong governmentsystem with various departments incl
  • Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    原文转载自 sunny2038 的CSDN博客文章 原文地址 最近在看Java xff0c 在编译写书上一个例子时 xff0c 由于书上的代码只有一部分 xff0c 于是就自己补了一个内部类 结果编译时出现 xff1a No enclosi
  • 瑞星微开发工具下载镜像的配置方法?

    如何根据 parameter txt 建立配置表 xff1f 首先我们来看下 parameter txt 的内容 xff1a CMDLINE mtdparts 61 rk29xxnand 0x00002000 64 0x00004000 u
  • 特别困的学生(Extraordinarily Tired Students)

    Extraordinarily Tired Students Time limit 3 000 seconds When a student is too tired he can 39 t help sleeping in class e
  • 骰子涂色(Cube painting)

    Cube painting We have a machine for painting cubes It is supplied with three different colors blue red and green Each fa
  • 盒子(Box)

    Box Time limit 3 000 seconds Ivan works at a factory that produces heavy machinery He hasa simple job he knocks up woode
  • 循环小数(Repeating Decimals)

    Repeating Decimals Time limit 3 000 seconds Repeating Decimals The decimal expansion of the fraction 1 33 is wherethe is