UVA332 LA5440 Rational Numbers from Repeating Fractions【循环小数转分数+GCD】

A rational number is any which can be written in the form p/q, where p and q are integers. All rational numbers less than 1 (that is, those for which p is less than q) can be expanded into a decimal fraction, but this expansion may require repetition of some number of trailing digits. For example, the rational number 7/22 has the decimal expansion .3181818… (note that the pair of digits 1 and 8 repeat ad infinitum). Numbers with such repeating decimal expansions are usually written with a horizontal bar over the repeated digits, like this: .318
If we are given the decimal expansion of a rational fraction (with an indication of which digits are repeated, if necessary), we can determine the rational fraction (that is, the integer values of p and q in p/q) using the following algorithm. Assume there are k digits immediately after the decimal point that are not repeated, followed by a group of j digits which must be repeated. Thus for 7/22 we would have k = 1 (for the digit 3) and j = 2 (for the digits 1 and 8). Now if we let X be the original number (7/22), we can compute the numerator and denominator of the expression
(10k+j × X − 10k × X) / (10k+j − 10k)
    For .318 we obtain the following calculation for the numerator of this fraction:
103 × .318 − 10^1 × .318 = 318.18 − 3.18 = 315
    The denominator is just 1000 − 10, or 990. It is important to note that the expression in the numerator and the denominator of this expression will always yield integer values, and these represent the numerator and denominator of the rational number. Thus the repeated fraction .318 is the decimal expansion of the rational number 315/990. Properly reduced, this fraction is (as expected) just 7/22.
Input
The input data for this problem will be a sequence of test cases, each test case appearing on a line by itself, followed by a ‘-1’. Each test case will begin with an integer giving the value of j, one or more spaces, then the decimal expansion of a fraction given in the form ‘0.ddddd’ (where d represents a decimal digit). There may be as many as nine (9) digits in the decimal expansion (that is, the value of k + j may be as large as 9).
Output
For each test case, display the case number (they are numbered sequentially starting with 1) and the resulting rational number in the form p/q, properly reduced.
Sample Input
2 0.318
1 0.3
2 0.09
6 0.714285
-1
Sample Output
Case 1: 7/22
Case 2: 1/3
Case 3: 1/11
Case 4: 5/7

Regionals 1994 >> North America - North Central NA

问题链接:UVA332 LA5440 Rational Numbers from Repeating Fractions
问题简述:(略)
问题分析
    这是一个循环小数转分数问题。需要进行约分运算,所以要计算最大公约数。
    小数转分数需要根据进制原理进行计算。搞不定上划线,原题中的上划线写为下划线。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA332 LA5440 Rational Numbers from Repeating Fractions */

#include 

using namespace std;

const int N = 128;
char s[N];

int gcd(int m, int n)
{
    return n ? gcd(n, m % n) : m;
}

int main()
{
    int j, caseno = 0;
    while(~scanf("%d", &j) && j!= -1) {
        scanf("%s", s);

        int a = 0, b = 1, c = 1;
        for(int k = 2; s[k]; k++) {
            a *= 10;
            a += s[k] - '0';
            b *= 10;        // 计算10的(k+j)次幂
            if(k <= j + 1)
                c *= 10;        // 计算10的j次幂
        }
        if(j) {
            a -= a / c;
            b -= b / c;
        }
        int g = gcd(a, b);
        a /= g;
        b /= g;

        printf("Case %d: %d/%d\n", ++caseno, a, b);
    }

    return 0;
}

你可能感兴趣的:(#,ICPC-备用二,#,ICPC-进制与分数,#,ICPC-数论:GCD/LCM,#,ICPC-UVALive,#,ICPC-UVA)