UVA202 UVALive5141 Repeating Decimals【分数】

本文介绍了一种通过编程方式确定分数循环小数及其循环节长度的方法。文章详细阐述了如何通过模拟手工计算过程来找出循环节,并给出了完整的C语言程序实现。

The decimal expansion of the fraction 1/33 is 0.03, where the 03 is used to indicate that the cycle 03repeats 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 nosuch 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.


  Write a program that reads numerators and denominators of fractions and determines their repeatingcycles.

  For the purposes of this problem, define a repeating cycle of a fraction to be the first minimal lengthstring of digits to the right of the decimal that repeats indefinitely with no intervening digits. Thusfor example, the repeating cycle of the fraction 1/250 is 0, which begins at position 4 (as opposed to 0which 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 integerdenominator, which is positive. None of the input integers exceeds 3000. End-of-file indicates the endof input.

Output

For each line of input, print the fraction, its decimal expansion through the first occurrence of the cycleto the right of the decimal or 50 decimal places (whichever comes first), and the length of the entirerepeating cycle.

  In writing the decimal expansion, enclose the repeating cycle in parentheses when possible. If theentire repeating cycle does not occur within the first 50 places, place a left parenthesis where the cyclebegins — 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

5/43 = 0.(116279069767441860465)

    21 = number of digits in repeating cycle

1/397 = 0.(00251889168765743073047858942065491183879093198992...)

    99 = number of digits in repeating cycle


World Finals >> 1990 - Washington


问题链接UVA202 UVALive5141 Repeating Decimals

问题简述

  输入两个整数numerator和denominator,分别为分子和分母。0≤分子,1≤分母≤3000。输出a/b的循环小数表示以及循环节长度。如果循环周期大于50,只显示50位,之后的全部用“...”表示

问题分析

  先取出整数部分(numerator/denominator的商),然后用余数numerator%denominator的余数)计算小数点后的各位。每次将余数先乘以10,就可以取出小数及其余数,循环往复。某两个位余数部分相同则表示,则表示这个区间小数点后的位形成循环

  该把该题归类为哪一类,有点困惑,也许归为模拟题是合适的,因为只要模拟人的计算过程就可以了。其实,这个题是一个数学题。

程序说明

  程序中,循环控制条件有点变态,但是是正确的。


AC的C语言程序如下:

/* UVA202 UVALive5141 Repeating Decimals */

#include <stdio.h>

#define MAXN 3000

int decimal[MAXN];
int numerator[MAXN];

int main(void)
{
    int n, d, start, end, i, j;

    while(scanf("%d%d", &n, &d) != EOF) {
        i = 0;
        numerator[i] = n % d;
        decimal[i] = numerator[i] * 10 / d;

        for(i=1; ;i++) {
            numerator[i] = numerator[i-1] * 10 % d;
            decimal[i] = numerator[i] * 10 / d;

            for(j=0; j<i; j++)
                if(numerator[j] == numerator[i])
                    break;
            if(j < i) {
                start = j;
                end = i - 1;
                break;
            }
            if(numerator[i] == 0) {
                start = i;
                end = i;
                break;
            }
        }

        printf("%d/%d = %d.", n, d, n / d);
        for(i=0; i<start; i++)
            printf("%d", decimal[i]);
        printf("(");
        if(end - start + 1 > 50) {
            for(i=start; i<start+50; i++)
                printf("%d", decimal[i]);
            printf("...");
        } else {
            for(i=start; i<=end; i++)
                printf("%d", decimal[i]);
        }
        printf(")\n");
        printf("   %d = number of digits in repeating cycle\n\n", end - start + 1);
    }

    return 0;
}


<think>好的,用户想找UVA202题的编程问题解决方案或解释。我需要先确认UVA202的具体内容。UVA的题目编号通常对应不同的题目,比如UVA202是循环小数问题。用户可能遇到了这个题目的困难,需要帮助理解或编写代码。 首先,我应该回忆一下UVA202题的要求。题目大致是给定两个整数a和b,计算a/b的循环小数表示,并确定循环节的长度。例如,1/6=0.1(6),循环节长度是1。这个问题的关键在于模拟除法过程,记录余数的出现位置,当余数重复时,就找到了循环节。 接下来,用户可能需要具体的解题思路和代码实现。解题步骤可能包括:处理整数部分,计算小数部分,使用哈希表记录余数的位置,检测循环的开始。需要注意处理整除的情况,比如4/2=2.0,没有循环节。此外,输出格式需要符合题目要求,比如显示一定长度的小数,并用括号标出循环节。 然后,我需要检查是否有常见的错误点。比如余数处理不正确,循环节判断错误,或者输出格式不符合要求。此外,边界情况比如分子为0、分母为负数等也需要考虑,不过题目可能规定输入范围,需要按照题目描述处理。 代码部分需要用Python或其他语言实现。例如,在Python中,可以用字典来记录余数的位置,每次计算余数并检查是否已经存在。当余数重复时,确定循环节的起始和结束位置,然后格式化输出。 最后,生成相关问题,帮助用户深入理解问题,比如循环小数判断的其他方法、高精度除法的实现,或者如何处理大数运算等。</think>### UVA202题解:循环小数 #### 题目理解 给定两个整数$a$和$b$,计算$a/b$的循环小数表示形式,并确定循环节的长度。例如: 输入:1 6 输出:1/6 = 0.1(6) 循环节长度 = 1 #### 算法思路 1. **整数部分处理**:计算$a \div b$的整数部分 2. **余数追踪**:使用哈希表记录余数出现的**位置**,当余数重复时即发现循环 3. **循环节检测**:余数归零则为有限小数,否则根据余数重复位置确定循环节 4. **输出格式化**:按题目要求显示**50位小数**并用括号标记循环节 #### Python实现代码 ```python def repeating_decimal(numerator, denominator): integer_part = numerator // denominator remainder = numerator % denominator seen = {} decimal = [] index = 0 while remainder != 0 and remainder not in seen: seen[remainder] = index remainder *= 10 decimal_digit = remainder // denominator decimal.append(str(decimal_digit)) remainder = remainder % denominator index += 1 if remainder == 0: return f"{integer_part}.{''.join(decimal)}(0)", 1 else: cycle_start = seen[remainder] non_repeating = decimal[:cycle_start] repeating = decimal[cycle_start:] return ( f"{integer_part}.{''.join(non_repeating)}({''.join(repeating)})", len(repeating) ) # 示例用法 numerator, denominator = 1, 6 result, length = repeating_decimal(numerator, denominator) print(f"{numerator}/{denominator} = {result}\n循环节长度 = {length}") ``` #### 关键点说明 1. **余数记录机制**:字典`seen`存储余数和对应的小数位位置 2. **循环终止条件**:余数为0或检测到重复余数 3. **输出截断规则**:题目要求最多显示50位小数,代码中需添加截断逻辑(示例代码未展示,实际实现需补充) 4. **负数处理**:需先处理符号后取绝对值计算,最后恢复符号 #### 测试案例验证 输入:5 7 输出: 5/7 = 0.(714285) 循环节长度 = 6 输入:1 3 输出: 1/3 = 0.(3) 循环节长度 = 1 输入:4 2 输出: 4/2 = 2.0(0) 循环节长度 = 1
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值