废话不多说 先来段代码
1 #include<stdio.h> 2 #include<math.h> 3 int main() 4 { 5 double x,y,temp; 6 int num; 7 while(scanf("%lf%lf",&x,&y)) 8 { 9 10 11 12 13 14 x=(x)/100;y=(y)/100; 15 for(num=1;;num++) 16 //if((num*x>=0)&&(num*y>1)&&((int)(num*x+0.0000000001)!=(int)(num*y+0.000000001))) 17 if(((int)(num*y)-(int)(num*x))>=1) 18 { 19 printf("%.15lf %.15lf\n",num*x,num*y); 20 printf("%d %d\n",(int)(num*x),(int)(num*y)); 21 break; 22 } 23 printf("%d\n",num); 24 } 25 26 return 0; 27 } 28 /*#include<stdio.h> 29 30 int main() 31 32 { 33 34 long int i;int flag=0; 35 36 double p,q; 37 38 39 while(scanf("%lf %lf",&p,&q)!=EOF) 40 { 41 42 p=(p+0.000000001)*1.0/100;q=(q-0.00000001)*1.0/100; 43 44 45 46 for(i=1;i<100000&&flag==0;i++) 47 48 { 49 50 if((int)((i*(q)))-(int)((i*(p)))>=1) 51 52 flag=1; 53 54 } 55 56 57 58 printf("%d\n",i-1); 59 flag=0; 60 } 61 62 return 0;*/
p=(p+0.000000001)*1.0/100;q=(q-0.00000001)*1.0/100;
这是关键的一步,思路没问题,精度有问题...
p=(p+0.000000001)*1.0/100;是double在多次运算后可能由(eg::由1变成0.9999999999,所以加上0.00000000000001)
q=(q-0.00000001)*1.0/100;是可能两个数乘以x,y分别得到 0.5000000 1.00000000,因为那上面的判断条件是<=1,所以可能有恰好在整数点的值,所以防止出现......