Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求助:用两个循环来搜索应该超时,为什么会是WA呢?

Posted by rainstorm at 2007-08-28 00:26:07 on Problem 1650
我已经优化了一下搜索时间:
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator