算法竞赛入门经典(第二版)-刘汝佳-第三章 数组与字符串 循环小数

http://acm.hust.edu.cn/vjudge/contest/123670#problem/N     密码5201



#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
//freopen("C:\\Users\\5201\\Desktop\\1.txt","r",stdin);
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		if(n%m==0)
		{
			printf("%d/%d = %d.(0)\n",n,m,n/m);
			printf("   1 = number of digits in repeating cycle\n\n");
			continue;
		}
		printf("%d/%d = %d.",n,m,n/m);
		int t=n%m;
		int a[100000]={};
		int b[100000]={};
		b[0]=n%m*10;
		a[0]=b[0]/m;
		b[0]%=m;
		if(t*10%m==0)
		{
			printf("%d(0)\n",t*10/m);
			printf("   1 = number of digits in repeating cycle\n\n");
			continue;
		}
		int len=1,i;
		while(1)
		{
			if( (b[len-1]*10)%m!=0 )
			{
				b[len]=(b[len-1]*10)%m;
				a[len]=(b[len-1]*10)/m;
				for(i=0;i<len;i++)
				{
					if(b[i]==b[len]&&a[i]==a[len]) goto out;
				}
				len++;
			}
			else
			{
				for(i=0;i<len;i++) printf("%d",a[i]);
				printf("%d",(b[len-1]*10)/m);
				printf("(0)\n");
				printf("   1 = number of digits in repeating cycle\n\n");
				goto end;
			}
		}
	out:
		int j;
		for(j=0;j<i;j++) printf("%d",a[j]);
		printf("(");
		for(j=i;j<len&&j-i<50;j++) printf("%d",a[j]);
		if(j-i>=50) printf("...");
		printf(")\n");
		printf("   %d = number of digits in repeating cycle\n\n",len-i);
	end:;
	}
}
思路:

1.输入整数部分和小数点。

2.用a保存小数的值。

3.用b保存除法过程中的余数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值