[ZJc084][UVA275] - Expanding Fractions

題目:
http://zerojudge.tw/ShowProblem?problemid=c084
------------------------------------------------------------------------------------------------------------------

#include<bits/stdc++.h>
#define maxn 1000+1
using namespace std;
int main()
{
int a,b;
while(scanf("%d%d",&a,&b)!=EOF && a || b)
{
int x = 0,s1[maxn]={0},s2[maxn]={0}, len = 0, z = 0;
s2[0] = a;
while(1)
{
z = 0;
x++;
s1[x] = a * 10 / b;
a = (a * 10) % b;
s2[x] = a;
if(!a)
{
len = -1;
break;
}
for(int i=0;i<x;i++)
{
if(s2[x] == s2[i])
{
z = 1;
len = x - i;
break;
}
}
if(z)
break;
}
printf(".");
for(int i=1;i<=x;i++)
{
printf("%d",s1[i]);
if((i + 1) % 50 == 0)
printf("\n");
}
printf("\n");
if(len > 0)
printf("The last %d digits repeat forever.\n",len);
if(len == -1)
printf("This expansion terminates.\n");
}
return 0;
}

留言

這個網誌中的熱門文章

[ZJ]a227: 三龍杯 -> 河內之塔

建立 python 環境(windows 8)

[ZJ]b513: 判斷質數-商競103