The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03 repeats 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 no such 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.

fraction  decimal expansion   repeating cycle   cycle length
1/6        0.1(6)       6         1

5/7       0.(714285)     714285        6

1/250      0.004(0)       0         1

300/31     9.(677419354838709)  677419354838709 15

655/990     0.6(61)       61         2

Write a program that reads numerators and denominators of fractions and determines their repeating cycles.
For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal length string of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thus for example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0 which 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 integer denominator, 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 the first occurrence of the cycle to the right of the decimal or 50 decimal places (whichever comes first), and the length of the entire repeating cycle. In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If the entire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cycle begins — 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


题意:输入整数a和b(0<=a、b<=3000),输出a/b的循环小数以及循环节长度,最多显示50位,超过50位后面的用“...”表示。输出循环节的小数位数


看到这道题目的时候是一脸懵圈呀,完全不知道它在说什么,连高精度小数都不会算。。。

在百度了过后发现了高精度小数的一个算法

a对b取余然后乘10除b直到除数重复,另取一个数组保存次数,那么除数重复的时候查看保存次数即可直到从第几项开始重复了

 memset(vis, -, sizeof(vis));
int c = a % b, cnt = ;
c *= ;
while(vis[c] == -)
{
res[cnt] = c / b;
vis[c] = cnt++;
c %= b;
c *= ;
}

其中vis数组用于保存一个被除数访问的次数

res数组用于保存小数值

cnt保存除法次数

那么为什么可以这么计算

下面来讲述一下这个算法的运行过程


首先,明确c/b为整数部分

然后将c*10的意义就是将小数点后移一位

一位一位的实现计算

从而可以实现不断计算小数位数的功能

下面贴出代码

源代码摘自:https://blog.csdn.net/flyawayl/article/details/51892740

 //#define LOCAL
#include <stdio.h>
#include <string.h>
const int maxn = + ; int a, b;
int vis[maxn], res[maxn]; int main() {
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
while(scanf("%d%d", &a, &b) == ) {
memset(vis, -, sizeof(vis));
int c = a % b, cnt = ;
c *= ;
while(vis[c] == -) {
res[cnt] = c / b;
vis[c] = cnt++;
c %= b;
c *= ;
}
// repeating cycle start-position
int sta_pos = vis[c];
printf("%d/%d = %d.", a, b, a/b);
for(int i = ; i < sta_pos; i++) {
printf("%d", res[i]);
}
printf("(");
if(cnt - sta_pos <= ) {
for(int i = sta_pos; i < cnt; i++) {
printf("%d", res[i]);
}
} else {
for(int i = sta_pos; i < sta_pos+; i++) {
printf("%d", res[i]);
}
printf("...");
}
printf(")\n");
printf(" %d = number of digits in repeating cycle\n\n", cnt - sta_pos);
}
return ;
}

2019-02-16  05:13:31  Author:LanceYu

UVa 202 Repeating Decimals 题解的更多相关文章

  1. UVa 202 Repeating Decimals(抽屉原理)

    Repeating Decimals 紫书第3章,这哪是模拟啊,这是数论题啊 [题目链接]Repeating Decimals [题目类型]抽屉原理 &题解: n除以m的余数只能是0~m-1, ...

  2. UVa 202 Repeating Decimals【模拟】

    题意:输入整数a和b,输出a/b的循环小数以及循环节的长度 学习的这一篇 http://blog.csdn.net/mobius_strip/article/details/39870555 因为n% ...

  3. UVa 202 - Repeating Decimals

    给你两个数,问你他们相除是多少,有无限循环就把循环体括号括起来 模拟除法运算 把每一次的被除数记下,当有被除数相同时第一个循环就在他们之间. 要注意50个数之后要省略号...每一次输出之后多打一个回车 ...

  4. uva 202(Repeating Decimals UVA - 202)

    题目大意 计算循环小数的位数,并且按照格式输出 怎么做 一句话攻略算法核心在于a=a%b*10,用第一个数组记录被除数然后用第二个数组来记录a/b的位数.然后用第三个数组记录每一个被除数出现的位置好去 ...

  5. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  6. 【习题 3-8 UVA - 202】Repeating Decimals

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 余数出现循环节. 就代表出现了循环小数. [代码] #include <bits/stdc++.h> using nam ...

  7. Repeating Decimals UVA - 202

    The / repeats indefinitely with no intervening digits. In fact, the decimal expansion of every ratio ...

  8. Repeating Decimals UVA - 202---求循环部分

    原题链接:https://vjudge.net/problem/UVA-202 题意:求一个数除以一个数商,如果有重复的数字(循环小数),输出,如果没有,输出前50位. 题解:这个题一开始考虑的是一个 ...

  9. UVa202 Repeating Decimals

    #include <stdio.h>#include <map>using namespace std; int main(){    int a, b, c, q, r, p ...

随机推荐

  1. JAVA反射(资源版)

    关于JAVA反射的作用可以看这篇博客(说的很详尽): https://www.cnblogs.com/jqyp/archive/2012/03/29/2423112.html 下面是关于JAVA反射的 ...

  2. java下实现压缩数据存取

    使用Apache Commons Compress实现压缩数据存取,支持格式有XZ,7z,tar,zip,jar,bzip2,gzip等. 例子在 https://commons.apache.org ...

  3. 使用vue-video-player插件实现视频播放

    来自于https://blog.csdn.net/abelethan/article/details/89016678博客 1下载插件==>npm install vue-video-playe ...

  4. unittest单元测试,基于java的junit测试框架

    import unittestclass study(unittest.TestCase): def testXia(self): self.assertEqual((3*4),20) def tes ...

  5. selenium五十行代码自动化爬取淘宝

    先看一下代码,真的只是五十行: # coding=gbk from selenium import webdriver import time options = webdriver.ChromeOp ...

  6. 创建一个任意大小的全色矩阵 python

    img = np.zeros((112,112,3))#cv2.imread('F:/project/Breast/InBreast/INBreast/outimgpatch/allnocalcifi ...

  7. 改名移动批量文件 shutil.copy

    import glob import os import shutil filePath = 'F:/project/Breast/InBreast/INBreast/imgout/' newFile ...

  8. mask-rcnn环境配置windows

    安装pycocotools 这个方法非常简便 但是需要先安装git,并且同时配置一下C++的工具 https://blog.csdn.net/qq_41271957/article/details/8 ...

  9. 用pip命令把python包安装到指定目录

    sudo pip install transforms3d --target=/usr/local/lib/python2.7/site-packages pip install transforms ...

  10. ES6 Set求两个数组的并集、交集、差集;以及对数组去重

    并集: const arr1 = [1, 2, 3, 2, 5]; const arr2 = [1, 4, 6, 8, 3]; // 将两个数组合并 const concatArr = [...arr ...