| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求助:用两个循环来搜索应该超时,为什么会是WA呢?我已经优化了一下搜索时间:
#include <iostream>
#include <math.h>
using namespace std;
#define LIMIT 1E-6
int main()
{
double a, min = 10000;
int i, j, l;
int n, d;
cin >> a;
cin >> l;
for(i = 1; i <= l; i++)
{
for(j = i * int(a); j <= l; j++)
{
if((a - j*1.0/i) < LIMIT)
{
if(fabs(a - j*1.0/i) > fabs(a - (j-1)*1.0/i))
{
if(fabs(a - (j-1)*1.0/i) < min)
{
min = fabs(a - (j-1)*1.0/i);
n = j - 1;
d = i;
}
}
else
{
if(fabs(a - j*1.0/i) < min)
{
min = fabs(a - j*1.0/i);
n = j;
d = i;
}
}
break;
}
}
}
cout << n << ' ' << d << endl;
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator