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 |
Re:这个不好说,不能简单的说精度不如。和数据和程序写法都有关系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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator