这种题要想到的细节比较多,所以要先有一个清析的思路,特别是边界最容易出问题,
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctype.h>
#include<vector>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<string>
#include<map>
#include<stack>
using namespace std;
int gcd(int big,int small)
{
if (big<small)
swap(big,small);
if (big%small==0)
return small;
return gcd(small,big%small);
}
int get(long double a,long double b,int k,long double div)
{
int i,j;
long double c=a/b;
i=1;
j=(int)(a/b)+10;
j*=a;
while (i<j)
{
int m=i+(j-i)/2;
if (fabs((long double)m/(long double)k-c)<div)
return m;
else if (fabs((long double)j/(long double)k - c)<fabs((long double)i/(long double)k-c))
i=m+1;
else
j=m-1;
}
//cout<<i<<endl;
return i;
}
int main()
{
int i,j,k;
int a,b;
//cout<<377.0/227.0-749.0/451.0<<' '<<93.0/56.0-749.0/451.0<<endl;
long double c;
while (cin>>a>>b)
{
int g=gcd(a,b);
a/=g;
b/=g;
long double fa=a;
long double fb=b;
long double div=1.0;
c=fa/fb;
//cout<<c<<endl;
long double aa,bb;
div=100000000;
for (i=1;i<(int)c+10;i++)
{
if (div>=fabs((double)i-c))
{
div=fabs((double)i-c);
j=i;
}
}
printf("%d/1\n",j); //第一个单独判断最好,不然后面混合在一起容易混。
for (i=2;i<b;i++)
{
j=get(fa,fb,i,div);
aa=j;
bb=i;
if (div>fabs((aa)/(bb)-c))
{//cout<<aa/bb<<' '<<div<<endl;
div=fabs((aa)/(bb)-c);
printf("%d/%d\n",j,i);
//cout<<div<<' '<<fabs(aa/bb-c)<<endl;
}
}
//if (n>1)
printf("%d/%d\n\n",a,b);
}
return 0;
}
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 3521 | Accepted: 1302 |
Description
Gainers 1498
Losers 902
As a person with a head for numbers, you'll notice that the anchorperson could have said ``Gainers outnumbered losers 5 to 3'', which is a more accurate approximation to what really happened. After all, the exact ratio of winners to losers is (to the nearest millionth) 1.660754, and he reported a ratio of 14 to 9, which is 1.555555, for an error of 0.105199; he could have said ``5 to 3'', and introduced an error of only 1.666667-1.660754=0.005913. The estimate ``5 to 3'' is not as accurate as ``1498 to 902'' of course; evidently, another goal is to use small integers to express the ratio. So, why did the anchorperson say ``14 to 9?'' Because his algorithm is to lop off the last two digits of each number and use those as the approximate ratio.
What the anchorman needs is a list of rational approximations of increasing accuracy, so that he can pick one to read on the air. Specifically, he needs a sequence {a_1, a_2, ..., a_n} where a_1 is a rational number with denominator 1 that most exactly matches the true ratio of winners to losers (rounding up in case of ties), a_{i+1} is the rational number with least denominator that provides a more accurate approximation than a_i, and a_n is the exact ratio, expressed with the least possible denominator. Given this sequence, the anchorperson can decide which ratio gives the best tradeoff between accuracy and simplicity.
For example, if 5 stocks rose in price and 4 fell, the best approximation with denominator 1 is 1/1; that is, for every stock that fell, about one rose. This answer differs from the exact answer by 0.25 (1.0 vs 1.25). The best approximations with two in the denominator are 2/2 and 3/2, but neither is an improvement on the ratio 1/1, so neither would be considered. The best approximation with three in the denominator 4/3, is more accurate than any seen so far, so it is one that should be reported. Finally, of course, 5/4 is exactly the ratio, and so it is the last number reported in the sequence.
Can you automate this process and help the anchorpeople?
Input
Output
The approximations for a pair are printed one to a line, beginning in column one, with the numerator and denominator of an approximation separated by a slash (``/''). A blank line separates one sequence of approximations from another.
Sample Input
5 4 1498 902
Sample Output
1/1 4/3 5/4 2/1 3/2 5/3 48/29 53/32 58/35 63/38 68/41 73/44 78/47 83/50 88/53 93/56 377/227 470/283 563/339 656/395 749/451
Source
207




被折叠的 条评论
为什么被折叠?



