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

为什么我枚举d错,改成枚举n才对?

Posted by qing1952003 at 2006-12-08 16:32:30 on Problem 1650
/*
#include <iostream>
#include <cmath>
using namespace std;

int main()
{int l,d,n,ansd,ansn;
 double a,min=100000000;
	scanf("%lf%d",&a,&l);
	ansd=ansn=1;
	for(d=1;d<=l;d++)
		{
			n=(int) d*a;
			if (n>l) continue;
			if (fabs(a-n*1.0/d)<min)
				{
					ansn=n;
					ansd=d;
					min=fabs(a-n*1.0/d);
				}
			n++;
			if (n>l) continue;
			if (fabs(a-n*1.0/d)<min)
				{
					ansn=n;
					ansd=d;
					min=fabs(a-n*1.0/d);
				}	
		}
	printf("%d %d\n",ansn,ansd);
return 0;
}
*/
上面是枚举d的!但是错了~下面改成n就对了!怎么回事?
#include <iostream>
#include <cmath>
using namespace std;

int main()
{int l,d,n,ansd,ansn;
 double a,min=100000000;
	scanf("%lf%d",&a,&l);
	ansd=ansn=1;
	for(n=1;n<=l;n++)
		{
			d=(int) n/a;
			if (d>l) continue;
			if (fabs(a-n*1.0/d)<min)
				{
					ansn=n;
					ansd=d;
					min=fabs(a-n*1.0/d);
				}
			d++;
			if (d>l) continue;
			if (fabs(a-n*1.0/d)<min)
				{
					ansn=n;
					ansd=d;
					min=fabs(a-n*1.0/d);
				}	
		}
	printf("%d %d\n",ansn,ansd);
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