自己写的一直WA啊。。搜了搜别人的换个思路写了
#include<stdio.h>
#include<math.h>
int main()
{
double a;
int l;
while (scanf("%lf", &a) != EOF)
{
scanf("%d", &l);
double min = 1000000, r;
int n = 1, d = 1, up, down;
while (n <= l && d <= l)
{
r = (double) n / (double) d;
if (fabs(a - r) < min)
{
min = fabs(a - r);
up = n;
down = d;
}
if (a == r)
break;
else if (r < a)
n++;
else
d++;
}
printf("%d %d\n", up, down);
}
return 0;
}