?UVa202
?
?方法:长除法
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define maxn 3000
int mer[maxn] = { 0 };//商
int rem[maxn] = { 0 };//余数
int arr[maxn] = { 0 };//标记重复的余数,出现重复代表无限循环小数
int main()
{
int dived, div;//被除数 除数
int i = 1;
int count = 0;//记录循环节大小
int not_cyc = 1;//标记是否循环
scanf("%d %d", &dived, &div);
int a = dived;//第一个被除数
int b = div;//第一个除数
int fir_mer = dived / div;//第一个商
while (dived)//dived为0代表是有限小数
{
dived = (dived % div) * 10;//上一个余数*10作为下一个的被除数
if (rem[dived] == 0)//余数不重复
{
rem[dived]++;
arr[i] = dived;//记录余数
}
else//余数重复,说明是无限循环小数
{
for (int j = 1; j < i; j++)//遍历寻找重复的余数
{
if (arr[j] == dived)//找到了
{
count = i - j;//相减得到循环节
break;
}
}
not_cyc = 0;//表示是否循环
break;
}
mer[i++] = dived / div;//记录商
}
if (!not_cyc)//循环进入
{
if (count <= 50)
{
printf("%d/%d = %d.(", a, b, fir_mer);
for (int j = 1; j < i; j++)
{
printf("%d", mer[j]);
}
printf(")\n ");
printf("%d = number of digits in repeating cycle\n", count);
}
else if (count > 50)
{
printf("%d/%d = %d.(", a, b, fir_mer);
for (int j = 1; j <= 50; j++)
{
printf("%d", mer[j]);
}
printf("...)\n ");
printf("%d = number of digits in repeating cycle\n", count);
}
}
else//不循环
{
printf("%d/%d = %d.", a, b, fir_mer);
for (int j = 1; j < i - 1; j++)
{
printf("%d", mer[j]);
}
printf("(0)\n ");
printf("1 = number of digits in repeating cycle\n");
}
return 0;
}