紫书 习题3-8 循环小数(Repeating Decimals,UVa202)

#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

const int MAX=3010;

int main()
{
    int m,n;
    while(scanf("%d %d",&m,&n)!=EOF)
    {
    	printf("%d/%d = %d.",m,n,m/n);
		int subscript[MAX],ans[MAX],count=0;
		memset(subscript,-1,sizeof(subscript));
		memset(ans,0,sizeof(ans));
		m%=n;
		while(subscript[m]==-1)
		{
			
			subscript[m]=count;
			ans[count]=m*10/n;
			m*=10;
			m%=n;
			count++;
		}
		for(int i=0;i<subscript[m];i++)
			printf("%d",ans[i]);
		printf("(");
		if(count-subscript[m]<51)
			for(int i=subscript[m];i<count;i++)
				printf("%d",ans[i]);
		else
		{
			for(int i=subscript[m];i<subscript[m]+50;i++)
				printf("%d",ans[i]);
			printf("...");
		}
		printf(")\n");
		printf("   %d = number of digits in repeating cycle\n\n",count-subscript[m]);
	}
	return 0;
}

你可能感兴趣的:(c++)