|
|
back to boardwhat is wrong ? Posted by evil 16 Apr 2002 11:52 #include <iostream.h> #include <math.h> void main() { float k; long i,l; cin>>k; {l=int(k)-1; for(i=2;i<=sqrt(k)+4;i++) if ((k/i==int(k/i))&& (i>=3) ){l=i- 1;break;}; cout<<l;}; } always get wrong answer Re: what is wrong ? > #include <iostream.h> > #include <math.h> > void main() > { float k; > long i,l; > cin>>k; > {l=int(k)-1; > for(i=2;i<=sqrt(k)+4;i++) if ((k/i==int(k/i))&& (i>=3) ){l=i- > 1;break;}; > cout<<l;}; > } Though I don't know what wrong about sqrt(k)+4 but I use k/2 instead of sqrt(k) and Get a AC try it please . I haven't learn C++ but I learned C and Pascal Why don't you use % in you program? If there is no % in C++ you can try this (k/i-int(k/i)<1e-15) and i think you'll get AC then. At last I give you a AC C program; #include<math.h> void main() { long n; long i; scanf("%ld",&n); for (i=3;i<=n/2;i++) if (!(n%i)) {printf("%ld\n",i-1);exit();} printf("%ld\n",n-1); } Good luck next time.:-) this is much faster > > #include <iostream.h> > > #include <math.h> > > void main() > > { float k; > > long i,l; > > cin>>k; > > {l=int(k)-1; > > for(i=2;i<=sqrt(k)+4;i++) if ((k/i==int(k/i))&& (i>=3) ){l=i- > > 1;break;}; #include<iostream.h> #include<math.h> int k,l; void main() { cin>>k; long temp=(long)floor(sqrt(k))+2;//be care about k=4 for(long i=3;i<=temp;i++) if(k%i==0){ cout<<i-1; return; } if(k%2==0)k/=2; cout<<k-1; } > > cout<<l;}; > > } > Though I don't know what wrong about > sqrt(k)+4 but > I use k/2 instead of sqrt(k) and Get a AC > try it please . > I haven't learn C++ but I learned C and Pascal > Why don't you use % in you program? > If there is no % in C++ you can try this > (k/i-int(k/i)<1e-15) > and i think you'll get AC then. > At last I give you a AC C program; > #include<math.h> > void main() > { > long n; > long i; > scanf("%ld",&n); > for (i=3;i<=n/2;i++) > if (!(n%i)) > {printf("%ld\n",i-1);exit();} > printf("%ld\n",n-1); > } > Good luck next time.:-) > |
|
|