题目意思还是很清楚的,要找一个分数使得它与x/y的差最小,要求分母b<n,b要最小,分子也最小的那个。
昨晚纠结了半天,还是没纠结出来,今天把它a了。。。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
double go(double xy,int a,int b)
{
double s=((double)a)/((double)b)-xy;
if(s>0)return s;
else return -s;
}
int main()
{
int x,y,n;
while(scanf("%d%d%d",&x,&y,&n)!=EOF)
{
double xy=((double)x)/((double)y);
int a,b,j=0;
double ss=110005,v;
for(int i=1;i<=n;i++)
{
while(go(xy,j,i)>go(xy,j+1,i))j++;
v=go(xy,j,i);
if(v<ss)
{
ss=v;
a=j;
b=i;
}
if(ss==0)break;
}
cout<<a<<"/"<<b<<endl;
}
return 0;
}