UVa 202 Repeating Decimals 题解

Posted lanceyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(")
");
42         printf("   %d = number of digits in repeating cycle

", cnt - sta_pos);
43     }
44     return 0;
45 }

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

以上是关于UVa 202 Repeating Decimals 题解的主要内容,如果未能解决你的问题,请参考以下文章

UVa 202 Repeating Decimals 题解

uva 202-Repeating Decimals

UVA202-Repeating Decimals

[算法竞赛入门经典]Repeating Decimals, ACM/ICPC World Finals 1990,UVa202

202 - Repeating Decimals

UVa202