這個題我的代碼是:
[cpp]
#include<cstdio>
#include<cmath>
using namespace std;
long long int gcd(long int a,long int b)
{
return a==0?b:gcd(b%a,a);
}
int main()
{
long long int x,y,n,mina,minb;
while(scanf("%I64d%I64d%I64d",&x,&y,&n)!=EOF)
{
int temp=gcd(x,y);
x/=temp;
y/=temp;
if(n>=y)
{
printf("%I64d/%I64d\n",x,y);
continue;
}
for(int i=1; i<=n; i++)
{
int a1=x*i/y;
int a2=a1+1;
if(i==1)
{
mina=a1;
minb=i;
}
if(fabs(a1*1.0/i-x*1.0/y)<fabs(mina*1.0/minb-x*1.0/y))
{
mina=a1;
minb=i;
}
if(fabs(a2*1.0/i-x*1.0/y)<fabs(mina*1.0/minb-x*1.0/y))
{
mina=a2;
minb=i;
}
}
temp=gcd(mina,minb);
printf("%I64d/%I64d\n",mina/temp,minb/temp);
}
return 0;
}
#include<cstdio>
#include<cmath>
using namespace std;
long long int gcd(long int a,long int b)
{
return a==0?b:gcd(b%a,a);
}
int main()
{
long long int x,y,n,mina,minb;
while(scanf("%I64d%I64d%I64d",&x,&y,&n)!=EOF)
{
int temp=gcd(x,y);
x/=temp;
y/=temp;
if(n>=y)
{
printf("%I64d/%I64d\n",x,y);
continue;
}
for(int i=1; i<=n; i++)
{
int a1=x*i/y;
int a2=a1+1;
if(i==1)
{
mina=a1;
minb=i;
}
if(fabs(a1*1.0/i-x*1.0/y)<fabs(mina*1.0/minb-x*1.0/y))
{
mina=a1;
minb=i;
}
if(fabs(a2*1.0/i-x*1.0/y)<fabs(mina*1.0/minb-x*1.0/y))
{
mina=a2;
minb=i;
}
}
temp=gcd(mina,minb);
printf("%I64d/%I64d\n",mina/temp,minb/temp);
}
return 0;
}
我用的是codeblocks編譯器,輸入7 6 3 輸出1/1,但是CF上提交後顯示如下:
應該怎麼改程序呢?