zoukankan      html  css  js  c++  java
  • 1408 盐水的故事-----一个值得研究的问题

    Problem Description
    挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下;然后滴二滴,停一下;再滴三滴,停一下...,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速度是一秒(假设最后一滴不到D毫升,则花费的时间也算一秒),停一下的时间也是一秒这瓶水什么时候能挂完呢?
     
    Input
    输入数据包含多个测试实例,每个实例占一行,由VUL和D组成,其中 0<D<VUL<5000。
     
    Output
    对于每组测试数据,请输出挂完盐水需要的时间,每个实例的输出占一行。
     
    Sample Input
    10 1
     
    Sample Output
    13

     这个问题有需要注意的地方:

    题目没有说明是整数所以要用双精度来做

    但是做的方法还是有所不同的

    我的代码:

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <stdio.h>
     4 #include <math.h>
     5 #include <string.h>
     6 #include <time.h>
     7 using namespace std;
     8 
     9 int main()
    10 {
    11     double num,d,j;
    12     int k,i,t;
    13     while(cin>>num>>d)
    14     {
    15         j=num/d;
    16         t=j;
    17         num=t;
    18         if(num!=j)
    19         k=t+1;
    20         else
    21         k=t;
    22         for(i=sqrt(k);;i++)
    23         {
    24             if(i*(i-1)/2<k&&i*(i+1)/2>=k)
    25             break;
    26         }
    27         cout<<k+i-1<<endl;
    28     }
    29     return 0;
    30 }
    View Code

    其他的代码

     1 #include<cstdio>
     2 #include<iostream>
     3 #define eps 1e-8
     4 using namespace std;
     5 int main(){
     6     double d,vul;
     7     int k,t;
     8     while(scanf("%lf%lf",&vul,&d)!=EOF){
     9         k=1;t=0;
    10         while(vul-k*d>eps){//滴完为止
    11             t+=k+1;
    12             vul-=k*d;
    13             k++;
    14         }
    15         if(vul-int(vul/d)*d<eps)
    16             t+=vul/d;
    17         else t+=vul/d+1;
    18         printf("%d
    ",t);
    19     }
    20     return 0;
    21 }
    View Code

    问题代码

     1 #include<stdio.h>
     2 int main()
     3 {
     4     double v,d;
     5     int count,i,j;
     6     while(~scanf("%lf%lf",&v,&d))
     7     {
     8         count=0;i=1;
     9         while(v>0.0)
    10         {
    11             if(i>1)count++;
    12             for(j=1;j<=i;j++)
    13             {
    14                 if(v>0.0)
    15                 {
    16                     v-=d;
    17                     count++;
    18                 }    
    19             }
    20             i++;
    21             //printf("%lf   %d
    ",v,count);
    22         }
    23         printf("%d
    ",count);
    24     }
    25     return 0;    
    26 }
    View Code

    这是一个AC的代码,但是问题就在这里了  我们用11 和1 输入    答案是15s   但是我们同时缩小  输入1.1和0.1   得到的答案就是16了
    然后我把这个问题修正了以后就WA了........

    修改以后

     1 #include<stdio.h>
     2 int main()
     3 {
     4     double v,d;
     5     int count,i;
     6     while(~scanf("%lf%lf",&v,&d))
     7     {
     8         count=0;i=1;
     9         while((v-d*i)>0.0)
    10         {
    11             v-=d*i;
    12             count+=i;
    13             i++;
    14             count++;
    15             //printf("%lf   %d
    ",v,count);
    16         }
    17         while(v>0.0)
    18         {
    19             v-=d;
    20             count++;
    21             printf("%lf
    ",v);
    22         }
    23         //printf("%d
    ",count);
    24     }
    25     return 0;    
    26 }
    View Code

    这个问题还没完全弄懂。。。。。

  • 相关阅读:
    c# 日期函数DateTime.ToString()日期的各种格式 (本人亲测)
    C# Excel导入、导出【源码下载】
    微信支付服务器CA证书更换服务器安装der证书的方法 DigiCert的根证书
    重置winsock目录解决不能上网的问题
    模型验证组件 FluentValidation
    对于“Newtonsoft.Json”已拥有为“NETStander.Library”定义的依赖项,解决办法
    .NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一)
    C# 中参数验证方式的演变
    一步一步ITextSharp 低级操作函数使用
    Winform 打印PDF顺序混乱,获取打印队列
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/5255774.html
Copyright © 2011-2022 走看看