UVa 202 Repeating Decimals 题解
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 题解的更多相关文章
- UVa 202 Repeating Decimals(抽屉原理)
Repeating Decimals 紫书第3章,这哪是模拟啊,这是数论题啊 [题目链接]Repeating Decimals [题目类型]抽屉原理 &题解: n除以m的余数只能是0~m-1, ...
- UVa 202 Repeating Decimals【模拟】
题意:输入整数a和b,输出a/b的循环小数以及循环节的长度 学习的这一篇 http://blog.csdn.net/mobius_strip/article/details/39870555 因为n% ...
- UVa 202 - Repeating Decimals
给你两个数,问你他们相除是多少,有无限循环就把循环体括号括起来 模拟除法运算 把每一次的被除数记下,当有被除数相同时第一个循环就在他们之间. 要注意50个数之后要省略号...每一次输出之后多打一个回车 ...
- uva 202(Repeating Decimals UVA - 202)
题目大意 计算循环小数的位数,并且按照格式输出 怎么做 一句话攻略算法核心在于a=a%b*10,用第一个数组记录被除数然后用第二个数组来记录a/b的位数.然后用第三个数组记录每一个被除数出现的位置好去 ...
- C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【习题 3-8 UVA - 202】Repeating Decimals
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 余数出现循环节. 就代表出现了循环小数. [代码] #include <bits/stdc++.h> using nam ...
- Repeating Decimals UVA - 202
The / repeats indefinitely with no intervening digits. In fact, the decimal expansion of every ratio ...
- Repeating Decimals UVA - 202---求循环部分
原题链接:https://vjudge.net/problem/UVA-202 题意:求一个数除以一个数商,如果有重复的数字(循环小数),输出,如果没有,输出前50位. 题解:这个题一开始考虑的是一个 ...
- UVa202 Repeating Decimals
#include <stdio.h>#include <map>using namespace std; int main(){ int a, b, c, q, r, p ...
随机推荐
- vue实现点击图标,图标在2s中完成旋转
<!-- 点击 vue实现点击图标,图标在2s中完成旋转 1==>如何让它在2s内完成旋转 使用动画 transform: rotate(-180deg); 动画的运动状态 transit ...
- 【转】带栗子的GDB教程
带栗子的GDB教程 原文链接:http://www.cprogramming.com/gdb.html作者:Manasij Mukherjee 一个好的调试软件是一个程序猿的工具箱里最重要的工具之一, ...
- 【声明式事务】Spring事务特性(二)
spring所有的事务管理策略类都继承自org.springframework.transaction.PlatformTransactionManager接口. 其中TransactionDefin ...
- LG3389 「模板」高斯消元法 高斯消元
问题描述 LG3389 题解 高斯消元,是用来解\(n\)元一次方程组的算法,时间复杂度\(O(n^3)\) 这样就构造出了这个方程组的矩阵 目标就是把这个矩阵左边\(n \times n\)消为单位 ...
- jQuery3.0+报错Uncaught TypeError: e.indexOf is not a function
jQuery3.0+报错Uncaught TypeError: e.indexOf is not a function 使用.load()绑定事件时报错,Uncaught TypeError: e.i ...
- layui的引用js踩坑
前言: 今天因为项目需要,需要使用layui,因为本身不多的前端经验,以为layui的用法和其他的前端框架,例如jquery,bootstrap等等,只需要直接引入layui.js,和layui.cs ...
- LOJ6029 [雅礼集训2017]市场
看到区间整除操作,直觉是不会除太多次就变成全 \(1\). 然而现在还有加操作. 我也不知道为什么,当一个节点的 \(\lfloor\frac{mx}{d}\rfloor=\lfloor\frac{m ...
- Shiro的基本使用
目录 前言 Shiro的介绍 Shiro功能 补充 HelloWorld 依赖包: 示例代码 代码解析 对于shiro.ini: 对于ShiroDemo.java: 补充 一些概念 Realm 认证 ...
- 运维相关指标数据采集并ES入仓 - 运维笔记
为了进行数字化IT治理,需要对一些应用进程相关指标进行采集并入库.收集到的应用指标数据最好要进行ES入仓,入到Kafka里面,并通过Kibana可视化展示. 需要进行采集的应用进程相关指标如下: ES ...
- python I/O多路复用 使用http完成http请求
1. 使用类实现比较方便我们使用里面的参数 2. 我们使用selector,不适用select from selectors import DefaultSelector 3. I/O多路复用是指使用 ...