[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;
}
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;
}
留言
張貼留言