• 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(")
    ");
    30         printf("   %d = number of digits in repeating cycle
    
    ",
    31         !n ? 1 : cnt - pos[n]);
    32     }
    33     return 0;
    34 }
    View Code
  • 相关阅读:
    闰年or平年判断
    输入一个日期判断是否正确的几种方法
    网页布局+下拉隐藏栏
    360导航布局
    [LeetCode] Longest Common Prefix
    [LeetCode] Length of Last Word
    [LeetCode] Valid Palindrome II
    [Qt] Qt信号槽
    [LeetCode] Split Linked List in Parts
    [LeetCode] Find Pivot Index
  • 原文地址:https://www.cnblogs.com/cyb123456/p/5777472.html
Copyright © 2020-2023  润新知