UVa202

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
Universidad de Valladolid OJ: 202 – Repeating Decimals 2/2
5/43 = 0.(116279069767441860465)
21 = number of digits in repeating cycle
1/397 = 0.(00251889168765743073047858942065491183879093198992...)
99 = number of digits in repeating cycle

题意:

给出两个数整数n(n>=0)和m(m>=1)。需要你给出n/m的结果(整数部分加小数点加包括循环节的小数部分)以及循环节的长度。

输入:

多组数据,每组一行两个整数n(n>=0)和m(m>=1)。

输出:

n/m的结果,整数部分加小数点加包括循环节的小数部分。如果是有限小数则循环节为0。最后还要输出循环节的长度。

分析:

模拟做除法的过程,分别开数组存储每次被除数被除数除得的商数(显然总是小于10的正整数)、每次被除数被除数除得的余数、以及出现过的余数第一次出现的位置。res[cnt]表示第cnt(从0开始)次除法所得到的商,yu[cnt]表示第cnt(从1开始)次除法所得到的余数,pos[num]表示余数num第一次出现是在第pos[num](>=1)次除法得到的余数中。不断进行下面的除法模拟过程:上一次得到的余数乘10再除以除数m得到这一次的商和余数,存储这一次除法所得到的商,判断这一次得到的余数之前是否出现过(循环节找到了)或者余数是否为零(已经除尽了),如果不是则不断重复该操作。注意数字只输出小于50位。

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 #define MAX_N 3000
 7 int res[MAX_N + 1],pos[MAX_N + 1],yu[MAX_N + 1];
 8 int main(){
 9     int n,m,t;
10     while (cin >> n >> m){
11         t = n;
12         memset(res,0,sizeof(res)),memset(pos,0,sizeof(pos));
13         int cnt = 0;
14         res[cnt++] = n / m,n = n % m;
15         while(!pos[n] && n){
16             pos[n] = cnt; // 存储上一次得到的余数出现的位置,从1开始
17             yu[cnt] = n;
18             res[cnt++] = 10 * n / m;
19             n = 10 * n % m;
20         }
21         printf("%d/%d = %d",t,m,res[0]);
22         printf(".");
23         for (int i = 1 ; i < cnt && i <= 50 ; ++i) {
24             if (n && yu[i] == n) printf("(");
25             printf("%d",res[i]);
26         }
27         if (!n) printf("(0");
28         if (cnt > 50) printf("...");
29         printf(")\n");
30         printf("   %d = number of digits in repeating cycle\n\n",
31         !n ? 1 : cnt - pos[n]);
32     }
33     return 0;
34 }

时间: 2024-07-31 22:22:50

UVa202的相关文章

UVA202 UVALive5141 Repeating Decimals

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

uva202:循环小数

题意: 给出两个数n,m,0<=n,m<=3000,输出n/m的循环小数表示以及循环节长度. 思路: 设立一个r[]数组记录循环小数,u[]记录每次的count,用于标记,小数计算可用 r[i]=n*10/m;n=n*10%10 直到n为0或u[n]!=0(找到循环节) 涉及到两个知识点:n/m的余数在0~m-1之间: 抽屉原理:循环次数最多不超过m+1次 具体见代码. //求循环节 #include<cstdio> #include<cstring> #define

UVa202 Repeating Decimals

#include <stdio.h>#include <map>using namespace std; int main(){    int a, b, c, q, r, places;    map<int, int> rmap;    pair<map<int, int>::iterator, bool> pr;    int qarr[50];    while (scanf("%d %d", &a, &

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

算法竞赛入门经典第二版第三章习题

写这个的原因是看到一位大神的习题答案总结,于是自己心血来潮也想写一个这个,目的主要是督促自己刷题吧,毕竟自己太弱了. 习题3-1 得分 UVa 1585 大致就是设置一个变量记录到当前为止的连续的O的数量,碰到X就变0,水题. #include<stdio.h> #include<ctype.h> #include<string.h> char s[90]; int main(void) { int length,n,sum,num; scanf("%d&qu

2019年2月做题记录

UVA10082 (字符串常量水题) UVA272 (字符串替换水题) UVA401 (回文串镜像串水题) UVA340 (模拟题) UVA1583 (打表水题) UVA1584 (暴力) UVA1585 (模拟) UVA1586 (数学) UVA1225 (打表水题) UVA455 (KMP算法) UVA232 (模拟+思维) UVA202 (除法高精度水题) UVA1587 (思维) UVA10340 (模拟,定序求交集) 原文地址:https://www.cnblogs.com/Aya-U