UVA - 202 Repeating Decimals
程序员文章站
2024-03-19 08:51:04
...
UVA - 202 Repeating Decimals
#include"cstdio"
#include"string.h"
int th[3005];
int ans[3005];
int main()
{
int a, b;
while (scanf("%d%d", &a, &b) != EOF)
{
int fir = a / b;
int x = (a % b) * 10;
int i = 0;
int len = 0;
int bre = 0;
while (1)
{
th[i] = x;//被除数
ans[i] = x / b;//运算的结果
int mod = x % b;//运算后的余数
for (int j = 0; j < i; ++j)
{
if (th[i] == th[j])//找到一个相同的被除数,后面就和前面某个部分一样了,可以判断循环了
{
bre = j;
len = i - j;
break;
}
}
if (len)break;
x = mod * 10; ++i;
}
printf("%d/%d = %d.", a, b, fir);
for (int i = 0; i < bre; ++i)
printf("%d",ans[i]);
printf("(");
if (len <= 50)
for (int i = bre; i < bre + len; ++i)
printf("%d", ans[i]);
else for (int i = bre; i < bre + 50; ++i)
{
printf("%d", ans[i]);
if (i == bre + 49)printf("...");
}
printf(")\n %d = number of digits in repeating cycle\n\n", len);
}
return 0;
}
上一篇: Golygons UVA - 225
下一篇: 贪吃蛇---C语言实现