题目描述

The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number(fraction) has a repeating cycle as opposed to decimal expansions of irrational numbers, which have nosuch repeating cycles.

Examples of decimal expansions of rational numbers and their repeating cycles are shown below. Here, we use parentheses to enclose the repeating cycle rather than place a bar over the cycle.

Write a program that reads numerators and denominators of fractions and determines their repeatingcycles.

For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal lengthstring of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thusfor example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0which begins at positions 1 or 2 and as opposed to 00 which begins at positions 1 or 4).

Input

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

Output

For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycleto the right of the decimal or 50 decimal places (whichever comes first), and the length of the entirerepeating cycle.
In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If theentire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cyclebegins — it will begin within the first 50 places — and place ‘…)’ after the 50th digit.

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

思路:循环小数的除数肯定是循环出现的,用一个数组储存除数,当除数再次出现时,停止循环.

#include<stdio.h>
#include<string.h>
int main(void)
{int n,m;int a[30005];//储存各位小数int b[30005];//储存除数出现的次数int c[30005];//某个除数刚出现时对应小数的位置while(scanf("%d%d",&n,&m)!=EOF){int sub=1;int p=n/m;printf("%d/%d = %d.",n,m,p);memset(b,0,sizeof(b));memset(c,0,sizeof(c));n=n%m*10;       //下一个被除数while(b[n]==0&&n){  //一旦被除数出现俩次  跳出循环b[n]=1;c[n]=sub;    //记录小数的位置a[sub]=n/m;   //记录小数sub++;n=n%m*10;}if(n==0){for(int i=1;i<sub;i++)printf("%d",a[i]);printf("(0)\n");}else {for(int i=1;i<c[n];i++)printf("%d",a[i]);printf("(");for(int i=c[n];i<sub&&i<=50;i++){printf("%d",a[i]);}if(sub>50)printf("...");printf(")\n");}if(n==0)printf("   1");else printf("   %d",sub-c[n]);printf(" = number of digits in repeating cycle\n\n");}return 0;
}

K - Repeating Decimals(循环小数)相关推荐

  1. UVA - 202 Repeating Decimals

    Repeating Decimals UVA - 202 题目传送门 解决方法:模拟一下除法,及时记录余数,当一个余数第二次出现时证明开始循环 AC代码 #include <cstdio> ...

  2. Repeating Decimals (计算循环小数)

    //计算循环小数,并找出循环节以及循环节的长度,最后输出时若循环节大于50最后输出省略号. //求循环小数,记录被除数,若被除数相同了则证明循环了. //注意:即使是循环小数,但可能并不是从第一位小数 ...

  3. UVa 202 Repeating Decimals

    计算循环小数的循环节 输入整数a和b(0<=a<=3000,1<=b<=3000),输出a/b的循环小数表示以及循环节长度. 例如,a=5,b=43,小数表示为0.(11627 ...

  4. UVa 202 - Repeating Decimals —— 分数循环节

    题目:计算分数的循环节. https://vjudge.net/contest/227853#problem/C 分析:数论,组合. n除以m的余数只能是0~m-1,根据抽屉原则,当计算m+1次时至少 ...

  5. LeetCode Longest Substring with At Least K Repeating Characters(递归)

    问题:给出一个字符串s,要求求出最长的子串,每个字符出现至少k次 思路: 方法一,先统计每个字符出现的次数,然后分别从头和尾找出出现资料最小为k的位置,然后再此区间遍历,当出现小于k次时,在两个子区间 ...

  6. leetcode 395. Longest Substring with At Least K Repeating Characters| 395. 至少有 K 个重复字符的最长子串(分治法)

    题目 https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/ 题解 参考:官方题解 ...

  7. UVa202Repeating Decimals (循环小数)

    解答: 因为除数是固定的,所以只要判断被除数是否出现过即可. #include<iostream> #include <cstdio> #include <cstring ...

  8. 395. Longest Substring with At Least K Repeating Characters

    题目要求 Find the length of the longest substring T of a given string (consists of lowercase letters onl ...

  9. HDU2522 A simple problem【分数与小数】

    A simple problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...

最新文章

  1. 工作事件五点作法和网络中所产生的Winsock连接与互动
  2. Zookeeper分布式一致性原理(五):Zookeeper-Java-API
  3. 无法访问你试图使用的功能所在的网络位置_[steam实用工具]解决无法访问商店/社区/好友列表的问题...
  4. Dotnet中Span, Memory和ReadOnlySequence之浅见
  5. php7.2.4 myspl5.7.21,linuapache php mysqlx下php开发环境搭建 Apache2 4 PHP7 1 MySQL5 7
  6. 《Beginning Linux Programming》读书笔记(四)
  7. 让VS2010支持Windows2000
  8. 网摘Android调用WebService
  9. 探讨6410的启动过程
  10. java encode_java的web开发中URLEncoder.encode方法
  11. cocos2d-x面试题(一)
  12. uva 437 巴比伦塔(DAG上dp)
  13. 目前主流的几种数字视频压缩编解码标准(转载)
  14. 国内主要的PDM产品关于浏览圈阅模块的总结(2006)
  15. Unity相机漫游脚本
  16. memcache1.5.8——LUR策略
  17. 第一台通用计算机发明时间,第一台计算机发明于哪一年?
  18. 神仙道各种公式【转载】
  19. html网站手机最小字体大小,html在手机浏览器如何设置字体大小
  20. 动手焊板子流水账-测温数码管电机联动

热门文章

  1. 3.4 java基础总结①常用类String②Stringbuffer③Stringbuilder
  2. Web常用字體-Font-Family
  3. 怎么用eclipse修改web工程的访问路径
  4. 快速阅读软件工程后的几个问题
  5. iOS小白之路...iOS中基础控件的使用
  6. 第三届蓝桥杯决赛c++b组
  7. 分享10款常用的jQuery焦点图插件
  8. Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExce
  9. 网络信息安全实验 — 网络攻击技术实验(Kali系统,John、lc7、arpspoof、ettercap、SQL注入...)
  10. 【数据库系统设计】关系数据库标准语言SQL(3)