您的位置:首页 > 其它

POJ 1930 Dead Fraction

2016-04-10 09:53 183 查看
要输出分母最小的分数。

所以需要枚举一下哪一个位置是循环的起点。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;

long long a,b;
long long f1,f2;
long long ans1,ans2;
char s[1000];

long long gcd(long long a, long long b)
{
if (a%b == 0) return b;
return gcd(b, a%b);
}

int main()
{
while(1)
{
scanf("%s",s);
int len=strlen(s);
if(len==1&&s[0]=='0') break;
ans2=99999999999999;
int pos;
for(int i=len-1;i;i--) if(s[i]!='.'){pos=i;break;}

for(int i=2;i<=pos;i++)
{
a=b=0; f1=1; f2=0;
for(int j=2;j<=i-1;j++)
{
a=a*10+(long long)(s[j]-'0');
f1=f1*10;
}
for(int j=i;j<=pos;j++)
{
b=b*10+(long long)(s[j]-'0');
f2=f2*10+9;
}
f2=f2*f1;

long long fz=a*f2+b*f1;
long long fm=f1*f2;

if(fm/gcd(fz,fm)<ans2)
{
ans1=fz/gcd(fz,fm);
ans2=fm/gcd(fz,fm);
}
}
printf("%lld/%lld\n",ans1,ans2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: