HDU1408 盐水的故事【模拟+数学】

盐水的故事

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23813    Accepted Submission(s): 5953


 

Problem Description

挂盐水的时候,如果滴起来有规律,先是滴一滴,停一下;然后滴二滴,停一下;再滴三滴,停一下...,现在有一个问题:这瓶盐水一共有VUL毫升,每一滴是D毫升,每一滴的速度是一秒(假设最后一滴不到D毫升,则花费的时间也算一秒),停一下的时间也是一秒这瓶水什么时候能挂完呢?

 

Input

输入数据包含多个测试实例,每个实例占一行,由VUL和D组成,其中 0<D<VUL<5000。

 

Output

对于每组测试数据,请输出挂完盐水需要的时间,每个实例的输出占一行。

扫描二维码关注公众号,回复: 2741389 查看本文章

 

Sample Input

 

10 1

 

Sample Output

 

13

 

Author

lcy

 

Source

杭电ACM集训队训练赛(IV)

问题链接HDU1408 盐水的故事

问题描述:(略)

问题分析

  这个题要基于模拟的思路来解。按一个一个周期往前走,每个周期滴n滴停一下。那么第n个周期时,盐水的数量Sn=n*(n+1)/2。第n个周期内,要考虑时间不是完全使用的情况就可以了。

程序说明

  给出2个程序,后一个程序没有考虑浮点数计算的精度问题,逻辑上是不够严格。前一个程序要好一些。

参考链接:(略)

题记:(略)

AC的C++语言程序如下:

/* HDU1408 盐水的故事 */

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

const double ESP = 1e-8;

int main()
{
    double d, vul;
    while(scanf("%lf%lf", &vul, &d) != EOF) {
        int ans, step = 1;
        for(;;) {
            step++;
            double cnt = d * (step * (step + 1));
            if(cnt - vul * 2 >= -ESP) {
                ans = (step * step+ 3 * step) / 2 - 1;
                while((cnt -= d * 2) - vul * 2 >= -ESP)
                    ans--;
                printf("%d\n", ans);
                break;
            }
        }
    }

    return 0;
}

AC的C++语言程序如下:

/* HDU1408 盐水的故事 */

#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

int main()
{
    double d, vul;
    while(scanf("%lf%lf", &vul, &d) != EOF) {
        int step = 1, last_step, ans;
        for(;;) {
            int k = step * (step + 1) / 2;
            double sum = d * k;
            last_step = step;
            if(sum == vul) {
                ans = k + step - 1;
                break;
            } else if(sum > vul) {
                int k = ((step - 1) - 1) * (step - 1) / 2;
                double sum = d * k;
                ans = k + last_step - 2;
                int x = ceil((vul - sum) / d);
                ans += x + 1;
                break;
            }
            step++;
        }

        printf("%d\n", ans);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/81627402