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 inde?nitely 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, de?ne a repeating cycle of a fraction to be the ?rst minimal length string of digits to the right of the decimal that repeats inde?nitely 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 ?le 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-?le indicates the end of input.

Output
For each line of input, print the fraction, its decimal expansion through the ?rst occurrence of the cycle to the right of the decimal or 50 decimal places (whichever comes ?rst), 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 ?rst 50 places, place a left parenthesis where the cycle begins — it will begin within the ?rst 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直到除数重复,另取一个数组保存次数,那么除数重复的时候查看保存次数即可直到从第几项开始重复了

 1 memset(vis, -1, sizeof(vis));
 2 int c = a % b, cnt = 0;
 3 c *= 10;
 4 while(vis[c] == -1)
 5 {
 6     res[cnt] = c / b;
 7     vis[c] = cnt++;
 8     c %= b;
 9     c *= 10;
10 }

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

res数组用于保存小数值

cnt保存除法次数

那么为什么可以这么计算

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



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

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

一位一位的实现计算

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

下面贴出代码

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

 1 //#define LOCAL
 2 #include <stdio.h>
 3 #include <string.h>
 4 const int maxn = 100000 + 5;
 5
 6 int a, b;
 7 int vis[maxn], res[maxn];
 8
 9 int main() {
10 #ifdef LOCAL
11     freopen("data.in", "r", stdin);
12     freopen("data.out", "w", stdout);
13 #endif
14     while(scanf("%d%d", &a, &b) == 2) {
15         memset(vis, -1, sizeof(vis));
16         int c = a % b, cnt = 0;
17         c *= 10;
18         while(vis[c] == -1) {
19             res[cnt] = c / b;
20             vis[c] = cnt++;
21             c %= b;
22             c *= 10;
23         }
24         // repeating cycle start-position
25         int sta_pos = vis[c];
26         printf("%d/%d = %d.", a, b, a/b);
27         for(int i = 0; i < sta_pos; i++) {
28             printf("%d", res[i]);
29         }
30         printf("(");
31         if(cnt - sta_pos <= 50) {
32             for(int i = sta_pos; i < cnt; i++) {
33                 printf("%d", res[i]);
34             }
35         } else {
36             for(int i = sta_pos; i < sta_pos+50; i++) {
37                 printf("%d", res[i]);
38             }
39             printf("...");
40         }
41         printf(")\n");
42         printf("   %d = number of digits in repeating cycle\n\n", cnt - sta_pos);
43     }
44     return 0;
45 }

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

原文地址:https://www.cnblogs.com/lanceyu/p/10386668.html

时间: 2024-10-08 01:40:21

UVa 202 Repeating Decimals 题解的相关文章

UVa 202 Repeating Decimals【模拟】

题意:输入整数a和b,输出a/b的循环小数以及循环节的长度 学习的这一篇 http://blog.csdn.net/mobius_strip/article/details/39870555 因为n%m的余数只可能是0到m-1中的一个,根据抽屉原理,当计算m+1次时至少存在一个余数相同 发现看了题解理解起来也好困难啊, 后来手动画了一下5/7的竖式除法的式子,,理解一些了 1 #include<iostream> 2 #include<cstdio> 3 #include<c

UVa 202 Repeating Decimals

计算循环小数的循环节 输入整数a和b(0<=a<=3000,1<=b<=3000),输出a/b的循环小数表示以及循环节长度. 例如,a=5,b=43,小数表示为0.(116279069767441860465),循环字节长度为21 可以用数组储存数字,模拟竖式除法来解决. 附AC代码: 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int MAX=3050;

uva 202 Repeating Decimals 模拟

弱校连萌题目链接:http://acm.bnu.edu.cn/v3/contest_show.php?cid=5772#problem/G 需要先想到出现循环节意味着出现了以前出现过的余数 然后就自己手写一个大数除法 后来看别人博客发现其实不用STL也可以 检查余数是否出现过可以是常数级的 因为除数不大于3000 所以开一个3010的数组来存余数标记即可 当然我还是用的map 因为复杂度实在太松了 这题烦得很 注意“输出前50位”是指所有数字加起来50位... 不是小数点后50位...(哭晕在厕

202 - Repeating Decimals

#include <stdio.h> #include <string.h> using namespace std; int isExist(int* list, int size, int val); int main(){ int m, n, divided[5000], count, idx, flag; char decimal[5000]; while(scanf("%d%d", &m, &n) != EOF){ count = 0,

LeetCode: Longest Substring Without Repeating Characters 题解

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

UVA202 UVALive5141 Repeating Decimals

问题链接:UVA202 UVALive5141 Repeating Decimals.基础训练级的问题,用C语言编写程序. 问题简述:输入两个整数numerator和denominator,分别为分子和分母.0≤分子,1≤分母≤3000.输出a/b的循环小数表示以及循环节长度.如果循环周期大于50,只显示50位,之后的全部用"..."表示. 解题思路:先取出整数部分(numerator/denominator的商),然后用余数(numerator%denominator的余数)计算小数

UVA202循环小数Repeating Decimals

Repeating Decimals The decimal expansion of the fraction 1/33 is , where the 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 cyc

Repeating Decimals UVA - 202

1 The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03 2 repeats indefinitely with no intervening digits. In fact, the decimal expansion of every rational number 3 (fraction) has a repeating cycle as

UVA 11729 Commando War 题解

“Waiting for orders we held in the wood, word from the front never came By evening the sound of the gunfire was miles away Ah softly we moved through the shadows, slip away through the trees Crossing their lines in the mists in the fields on our hand