UVA275 LA5384 POJ1140 Expanding Fractions循环节

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA275 LA5384 POJ1140 Expanding Fractions循环节相关的知识,希望对你有一定的参考价值。

Expanding Fractions
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3838 Accepted: 1468

Description

In this problem you are to print the decimal expansion of a quotient of two integers. As you well know, the decimal expansions of many integer quotients result in decimal expansions with repeating sequences of digits. You must identify these. You will print the decimal expansion of the integer quotient given, stopping just as the expansion terminates or just as the repeating pattern is to repeat itself for the first time. If there is a repeating pattern, you will say how many of the digits are in the repeating pattern.

Input

There will be multiple input instances, each instance consists of two positive integers on a line. The first integer represents the numerator of the fraction and the second represents the denominator. In this problem, the numerator will always be less than the denominator and the denominator will be less than 1000. Input terminates when numerator and denominator are both zero.

Output

For each input instance, the output should consist of the decimal expansion of the fraction, starting with the decimal point. If the expansion terminates, you should print the complete decimal expansion. If the expansion is infinite, you should print the decimal expansion up to, but not including the digit where the repeated pattern first repeats itself.

For instance, 4/11 = .3636363636…, should be printed as .36. (Note that the shortest repeating pattern should be found. In the above example, 3636 and 363636, among others, are repeating patterns, but the shortest repeating pattern is 36.)

Since some of these expansions may be quite long, multiple line expansions should each contain exactly 50 characters on each line (except the last line, which, of course, may be shorter) - that includes the beginning decimal point.

On the line immediately following the last line of the decimal expansion there should be a line saying either “This expansion terminates.”, or “The last n digits repeat forever.”, where n is the number of digits in the repeating pattern.

Helpful hint: The number of digits before the pattern is repeated will never be more than the value of the denominator.

Sample Input

3 7
345 800
112 990
53 122
0 0

Sample Output

.428571
The last 6 digits repeat forever.
.43125
This expansion terminates.
.113
The last 2 digits repeat forever.
.4344262295081967213114754098360655737704918032786
885245901639
The last 60 digits repeat forever.

Source

East Central North America 1994

问题链接UVA275 LA5384 POJ1140 Expanding Fractions
问题简述:给定分数的分子和分母计算其循环节。
问题分析:循环节有关的问题,不解释。
程序说明:POJ的输出格式与UVA略有不同,每个测试样例之间没有分隔行。
参考链接:(略)
题记:(略)

AC的C++语言程序(UVA和LA)如下:

/* UVA275 LA5384 Expanding Fractions */

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

int search(vector<int> &a, const int &n)
{
    for (int i = 0; i < a.size(); i++)
        if (n == a[i]) return i + 1;
    a.push_back(n);
    return 0;
}

int main()
{
    int n, m;
    while (~scanf("%d%d", &n, &m) && (n || m)) {
        printf(".");
        if (n == m || n == 0)
            printf("\\nThis expansion terminates.\\n\\n");
        else {
            vector<int> a;
            n *= 10;
            a.push_back(n);
            for (int i = 2; ;i++) {
                printf("%d", n / m);
                n %= m;
                if (n == 0) {
                    printf("\\nThis expansion terminates.\\n\\n");
                    break;
                }
                n *= 10;
                if (i <= m + 1) {
                    int k = search(a, n);
                    if (k) {
                        printf("\\nThe last %d digits repeat forever.\\n\\n", a.size() - k + 1);
                        break;
                    }
                }
                if (i % 50 == 0) printf("\\n");
            }
        }
    }

    return 0;
}

AC的C++语言程序(POJ)如下:

/* POJ1140 Expanding Fractions */

#include <iostream>
#include <vector>
#include <cstdio>

using namespace std;

int search(vector<int> &a, const int &n)
{
    for (int i = 0; i < a.size(); i++)
        if (n == a[i]) return i + 1;
    a.push_back(n);
    return 0;
}

int main()
{
    int n, m;
    while (~scanf("%d%d", &n, &m) && (n || m)) {
        printf(".");
        if (n == m || n == 0)
            printf("\\nThis expansion terminates.\\n");
        else {
            vector<int> a;
            n *= 10;
            a.push_back(n);
            for (int i = 2; ;i++) {
                printf("%d", n / m);
                n %= m;
                if (n == 0) {
                    printf("\\nThis expansion terminates.\\n");
                    break;
                }
                n *= 10;
                if (i <= m + 1) {
                    int k = search(a, n);
                    if (k) {
                        printf("\\nThe last %d digits repeat forever.\\n", a.size() - k + 1);
                        break;
                    }
                }
                if (i % 50 == 0) printf("\\n");
            }
        }
    }

    return 0;
}

以上是关于UVA275 LA5384 POJ1140 Expanding Fractions循环节的主要内容,如果未能解决你的问题,请参考以下文章

HDU1416 POJ1078 UVA653 LA5507 GizilchDFS

UVA12081 LA3413 POJ2769 Reduced ID Numbers同余

UVA619 LA5465 POJ1312 HDU1314 ZOJ1272 Numerically Speaking大数+进制

UVA326 LA5434 POJ1538 Extrapolation Using a Difference Table二项式

UVA557 LA5578 Burger概率

UVA280 LA5588 VertexDFS