zoukankan      html  css  js  c++  java
  • zoj1494 暴力模拟 简单数学问题

    Climbing Worm

    Time Limit: 2 Seconds      Memory Limit:65536 KB

    An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We'll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we'll assume the worm makes it out.


    Input

    There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end of output.


    Output

    Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the well.


    Sample Input

    10 2 1
    20 3 1
    0 0 0


    Sample Output

    17
    19

    题目大意:

    一只蜗牛要从爬上n英寸高的地方,他速度为u每分钟,他爬完u需要休息1分钟,且他休息时下滑d英寸,问他什么时候爬出去。

    数据小,可以模拟。

    while(~scanf("%d%d%d",&n,&u,&d),n||u||d)
    	{
    		int h=0;
    		int ans=0;
    		while(h<n)
    		{
    			ans++;
    			h+=u;
    			if(h >=n)
    				break;
    			ans++;
    			h-=d;
    		}
    		printf("%d
    ",ans);
    	}

    当数据大起来时,不妨想想公式。

    个人思路,感谢指点纠正。

    1:答案肯定是奇数(因为第一次是向上,最后一次也是向上,当然中间向下是偶数次)。

    2:第一次单出来,后面以一上爬加一下滑为一个单位。


    如 10 3 1                                            上行       汇总

                    第一天:                              3           3

                   第二,三天                            2           5

                   第四,五天                            2           7

                   第六,七天                            2           9

                   第八,九天                            2           11(到达)

    因为2*n和2*n+1(2*n-1还未到达)是先下后上,所以不会出现2*n就到到的情况。

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<math.h>
    #include<string>
    using namespace std;
    int a,b,c;
    void _solve(){
    	int ans;
    	ans=1;
    	a-=b;
    	if(a<0) {cout<<ans<<endl;return ;}//第一天结束 
    	int temp;
    	if(a%(b-c)==0) temp=a/(b-c);
    	else temp=a/(b-c)+1;//尾巴任然算一个单位 
    	temp*=2;//除了第一天,后面两天为一个单位 
    	ans+=temp;
    	cout<<ans<<endl;return ;
    }
    int main()
    {
         while(cin>>a>>b>>c)
           if(!a&&!b&&!c) return 0;
           else _solve();
    	 return 0;
    }



  • 相关阅读:
    HDU--2024
    HDU--2021
    HDU--2020
    HDU--2019
    HDU--2018
    HDU--2017
    HDU--2016
    python全栈开发day13-迭代器、生成器、列表推导式等
    python全栈开发day12-函数的有用信息、带参数的装饰器、多个装饰器装饰一个函数、global和nonlocal的进一步解析和总结
    Python3使用Print输出带颜色字体
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7603967.html
Copyright © 2011-2022 走看看