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. vue-router 之 keep-alive路由缓存处理include+exclude

    keep-alive 简介 keep-alive 是 Vue 内置的一个组件,可以使被包含的组件保留状态,或避免重新渲染. 用法也很简单: <keep-alive> <compone ...

  2. httpd虚拟主机起不来!!

    前几天在公司,练习负载均衡配置.在配置虚拟主机的web服务(apache) ,创建好虚拟主机的配置文件 ss -tnl  查看监控端口80已起来,通过本地浏览器访问一直显示默认的欢迎页... 一个下午 ...

  3. c# WF 第1节 创建winform程序

    本节内容: 1:vs的RAD 2:WinForm的创建简介 3:创建窗口与控制台程序文件的对比 4:窗口文件内容 5:winform怎么运行 6:winform的实质 1:vs的RAD 2:WinFo ...

  4. 富文本编辑器Simditor

    文档地址:https://simditor.tower.im/docs/doc-usage.html 父组件: options: { placeHolder: 'this is placeHolder ...

  5. 深度学习框架gpu安装方法

    1.tensorflow pip install tensorflow-gpu==1.14.0,具体安装哪一个版本,可以把1.14.0随便填写一个数字,系统会提示可以有哪些版本可以安装 2.pytor ...

  6. ora-01489 字符串连接的结果过长 解决方案

    如下代码,使用listagg进行分组拼接时,常常会报 ora-01489 错误,造成该报错的主要原因是:oracle对字符变量的长度限制,正常情况下,oracle定义的varchar2类型变量的长度不 ...

  7. [C1W2] Neural Networks and Deep Learning - Basics of Neural Network programming

    第二周:神经网络的编程基础(Basics of Neural Network programming) 二分类(Binary Classification) 这周我们将学习神经网络的基础知识,其中需要 ...

  8. 牛客OI周赛13-提高组-0还是1-(dp+位运算)

    https://ac.nowcoder.com/acm/contest/2970/A 给出长度为n的一连串位运算符号,用n+1个0或1使运算插入最后得到1,求01序列有多少种可能. dp[i][j]表 ...

  9. java jvm虚拟机类加载过程

    加载 在加载阶段, 虚拟机需要完成以下3件事情:1) 通过一个类的全限定名来获取定义此类的二进制字节流.2) 将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构.3) 在内存中生成一个代表这 ...

  10. git 关联vs code

    {     "window.zoomLevel": 1,     "editor.fontSize": 15,     "gitlens.advanc ...