zoukankan      html  css  js  c++  java
  • HDU-盐水的故事

    http://acm.hdu.edu.cn/showproblem.php?pid=1408

    这是一道高精度问题:

    在自己错了数十遍之后找到了不少规律:

    首先是Output limit exceeded:之前一直不知道是什么意思,现在才知道是输入输出的精度有问题

     1 /* */
     2 # include <bits/stdc++.h>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     long long int VUL, D, i, t, f;
     8     while( ~ scanf("%lld %lld", &VUL, &D) )
     9     {
    10         t=0;
    11         for( i=1; ; i++ )
    12         {
    13             f = i*D;
    14             if( VUL>f )
    15             {
    16                 t += i;
    17                 t += 1;
    18             }
    19             else if( VUL==f )
    20             {
    21                 t += i;
    22                 break;
    23             }
    24             else
    25             {
    26                 if( VUL%D==0 )
    27                 {
    28                     t += VUL/D;
    29                     break;
    30                 }
    31                 else
    32                 {
    33                     t += VUL/D+1;
    34                     break;
    35                 }
    36             }
    37             VUL = VUL - i * D;
    38         }
    39         printf("%lld
    ", t);
    40     }
    41     return 0;
    42 }
    View Code

    然后就是中间代码的精度问题了:

    记住比较大小时,double类型跟double类型的比,int类型跟int类型的比,double要是跟int类型的比会有精度缺失

    以下是AC代码:

     1 /* */
     2 # include <bits/stdc++.h>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     long long int  i, t;
     8     double VUL, D, f;
     9     while( ~ scanf("%lf %lf", &VUL, &D) )
    10     {
    11         t=0;
    12         for( i=1; ; i++ )
    13         {
    14             int p = (int)(VUL/D);///double 类型求余数
    15             double r = VUL - p * D;///double 类型求余数
    16             f = i*D*1.0;
    17             if( VUL>f )
    18             {
    19                 t += i;
    20                 t += 1;
    21             }
    22             else if( VUL==f )
    23             {
    24                 t += i;
    25                 break;
    26             }
    27             else if( r>=0.000001 )///不能跟0比,(即不能用r>0来判)
    28             {
    29                 t += p+1;
    30                 break;
    31             }
    32             else
    33             {
    34                 t += p;
    35                 break;
    36             }
    37             VUL = VUL - f;
    38         }
    39         printf("%lld
    ", t);
    40     }
    41     return 0;
    42 }
    View Code

    注意观察以下WA代码和上面的AC代码:可以发现(r==0.000000)是错误的,是不是0.000000==0呀,它也是int类型的?(欢迎大佬评论)

     1 /* */
     2 # include <bits/stdc++.h>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     long long int  i, t;
     8     double VUL, D, f;
     9     while( ~ scanf("%lf %lf", &VUL, &D) )
    10     {
    11         t=0;
    12         for( i=1; ; i++ )
    13         {
    14             int p = (int)(VUL/D);
    15             double r = VUL - p * D;
    16             f = i*D*1.0;
    17             if( VUL>f )
    18             {
    19                 t += i;
    20                 t += 1;
    21             }
    22             else if( VUL==f )
    23             {
    24                 t += i;
    25                 break;
    26             }
    27             else if( r==0.000000 )
    28             {
    29                 t += p;
    30                 break;
    31             }
    32             else
    33             {
    34                 t += p+1;
    35                 break;
    36             }
    37             VUL = VUL - f;
    38         }
    39         printf("%lld
    ", t);
    40     }
    41     return 0;
    42 }
    View Code

    PS.另一种AC代码:

     1 #include<stdio.h>
     2 int main()
     3 {
     4     double v,d;
     5     long long int t;
     6     while(scanf("%lf%lf",&v,&d)!=EOF)
     7     {
     8         int i;
     9         t=0;
    10         for(i=1;; i++)
    11         {
    12             if(v-i*d<=0)
    13             {
    14                 while(v>0.0000001)/*v与0比较的话就WA了,因为d可能很小很小,而d无论多小,v只要剩下就得算*/
    15                 {
    16                     v=v-d;
    17                     t++;
    18                 }
    19                 break;
    20             }
    21             v=v-i*d;
    22             t=t+i;
    23             t++;
    24 
    25         }
    26         printf("%lld
    ",t);
    27     }
    28     return 0;
    29 }
    View Code
  • 相关阅读:
    Redux React-redux 理解
    React ---- 浅谈ReactJs
    javascript --- 鼠标事件
    javaScript --- localStorage 本地存储
    javascript -- 对象(深拷贝、浅拷贝)
    javascript --- 数组输出数组中最大值最小值差值
    Css中未知 div 宽高度,水平垂直居中方法
    Windows下使用NCL(Cygwin模拟Linux环境)
    Qt加载网页(加载浏览器插件)和制作托盘后台运行(南信大财务报账看号)
    OpenGL超级宝典第5版&&GLSL法线变换
  • 原文地址:https://www.cnblogs.com/wsy107316/p/11079287.html
Copyright © 2011-2022 走看看