UVa 202 Repeating Decimals

本文详细介绍了如何从输入的整数分子和正整数分母中,计算并输出对应的分数及其十进制展开形式,包括循环节的长度。通过实例演示了完整的算法流程,确保结果准确无误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Input
Each line of the input le 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- le indicates the end
of input.
Output
For each line of input, print the fraction, its decimal expansion through the rst occurrence of the cycle
to the right of the decimal or 50 decimal places (whichever comes rst), 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 rst 50 places, place a left parenthesis where the cycle
begins | it will begin within the rst 50 places | and place `
...)
' after the 50th digit.
SampleInput
76 25
5 43
1 397
SampleOutput
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
#include <iostream>
#include <cstdlib>
#include <string.h>
#include <cstdio>

int s[3001];
int m[3001];

int main()
{
    int a, b;
    int temp;
    while(scanf("%d %d", &a, &b) == 2)
    {
    	temp = a;
    	int flag = 1;
    	int count = 0;
    	int num = 1;
    	s[0] = a / b;  //整数数列 
    	m[0] = a % b;  //余数数列 
    	printf("%d/%d = ", a, b);
    	printf("%d.", s[0]);
    	if(m[0] != 0)
    	{	
			for(int i = 0; flag == 1; i++)
			{
				a = m[i];
				a = a * 10;
				s[i+1] = a / b;
				m[i+1] = a % b;
				for(int j = 0; j < i+1; j++)
				{
					if(m[i+1] == m[j])
					{
						count = j+1;
						flag = 0;
						break;
					}
				}
				num++;
			}
			for(int i = 0,j = 0; i < num-1; i++, j++)
			{
				if(j > 49 && i < num - 1)
				{
					printf("...");
					break;
				}
				if(i == count-1)
				  printf("(");
				printf("%d", s[i+1]);
			}
			printf(")\n");
			printf("   %d = number of digits in repeating cycle\n\n", num - count);
		}
		else
		{
			printf("(%d)\n", 0);
			printf("   %d = number of digits in repeating cycle\n\n", 1);
		}		
	}	
	return 0;	
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值