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 |
为什么我枚举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(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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator