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

Re:这个不好说,不能简单的说精度不如。和数据和程序写法都有关系

Posted by noskill at 2005-04-21 16:52:29 on Problem 1079
In Reply To:Re:这个不好说,不能简单的说精度不如。和数据和程序写法都有关系 Posted by:noskill at 2005-04-21 12:30:30
我换了写法,终于C++和G++都AC了^_^
#include<stdio.h>
#include<math.h>
int main()
{
  int i,j,k1,k2,a,b,m=0;
  double r,t1,t2,rn;
  while(scanf("%d %d",&a,&b)==2)
  {
	if(m)printf("\n");
	r=(double)a/b;
	j=0;rn=r;j++;
	k1=(int)floor(r*j);k2=(int)ceil(r*j);
	  t1=(double)k1/j;t2=(double)k2/j;
	  if(fabs(t1-rn)<fabs(t2-rn))
	    {printf("%d/%d\n",k1,j);rn=fabs(t1-r);}
	  else {printf("%d/%d\n",k2,j);rn=fabs(t2-r);}
	j++;
	while(j<=b)
	{
	  k1=(int)floor(r*j);k2=(int)ceil(r*j);
	  t1=(double)k1/j;t2=(double)k2/j;
	  if(fabs(t1-rn)<fabs(t2-rn)&&fabs(t1-r)<rn)
	    {printf("%d/%d\n",k1,j);rn=fabs(t1-r);}
	  else if(fabs(t2-r)<rn)
	    {printf("%d/%d\n",k2,j);rn=fabs(t2-r);}
	  j++;
	}
	m++;
  }    
  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