Everyone says I'm out of question, "the old is not serious," Well, I'll return to a decent, I am author "serious" have to tell you that this is a problem, and this question is difficult.
Sequentially incremented tell you n (1 <= n <= 1000000) positive integers (each a positive integer are less than 2 31), and a positive real number P (0.000001 <= P <= 10 . 8), required to obtain the number of columns the proportion of the two closest to p number of a, b (i.e., taking two numbers a, b, and a, b may be the same, such fabs ((double) a / b - p) min), and treated-fraction representation. For example, the final answer is 4/6, 2/3, if the output; for example, the final answer is 3/3, 1/1 output.
Input
有多组数据,每组数据有3行:第一行一个正整数n,表示给出数列中数的个数;第二行n个正整数,
并且数列递增,两个数之间用一个空格隔开;第三行一个正实数p。
Output
每组数据对应一个输出,表示要求的那个分数,a/b最简分数形式。
Sample Input
5
1 4 5 6 7
0.666
Sample Output
2/3
#include<bits/stdc++.h>
using namespace std;
const double EPS=1e-6;
const int MAXN=1000005;
int n,l,r;
long long a[MAXN],up,down;
double x;
int main()
{
while(scanf("%d",&n)!=EOF)
{
up=down=0;
for(int i=1;i<=n;++i)
{
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n);
scanf("%lf",&x);
int r=1;
for(int i=1;i<=n;++i)
{
while((double)a[i]/(double)a[r]>x&&r<n)++r;
if(!down||abs(x-(double)a[i]/(double)a[r])<abs(x-(double)up/(double)down))
{
up=a[i];
down=a[r];
}
if(r>1&&(!down||abs(x-(double)a[i]/(double)a[r-1])<abs(x-(double)up/(double)down)))
{
up=a[i];
down=a[r-1];
}
}
long long g=__gcd(up,down);
up/=g;
down/=g;
printf("%lld/%lld\n",up,down);
}
return 0;
}