Uva 573

来源:互联网 发布:开淘宝网需要垫资金吗 编辑:程序博客网 时间:2024/04/29 19:26
The Snail

A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can climb 3 feet while the sun is up, but slides down 1 foot at night while sleeping. The snail has a fatigue factor of 10%, which means that on each successive day the snail climbs 10% $\times$ 3 = 0.3 feet less than it did the previous day. (The distance lost to fatigue is always 10% of the first day's climbing distance.) On what day does the snail leave the well, i.e., what is the first day during which the snail's height exceeds 6 feet? (A day consists of a period of sunlight followed by a period of darkness.) As you can see from the following table, the snail leaves the well during the third day.

 

Day Initial Height Distance Climbed Height After Climbing Height After Sliding 1 0' 3' 3' 2' 2 2' 2.7' 4.7' 3.7' 3 3.7' 2.4' 6.1' -

Your job is to solve this problem in general. Depending on the parameters of the problem, the snail will eventually either leave the well or slide back to the bottom of the well. (In other words, the snail's height will exceed the height of the well or become negative.) You must find out which happens first and on what day.

 

Input

The input file contains one or more test cases, each on a line by itself. Each line contains four integersH, U, D, and F, separated by a single space. If H = 0 it signals the end of the input; otherwise, all four numbers will be between 1 and 100, inclusive. H is the height of the well in feet, U is the distance in feet that the snail can climb during the day, D is the distance in feet that the snail slides down during the night, and F is the fatigue factor expressed as a percentage. The snail never climbs a negative distance. If the fatigue factor drops the snail's climbing distance below zero, the snail does not climb at all that day. Regardless of how far the snail climbed, it always slides D feet at night.

 

Output

For each test case, output a line indicating whether the snail succeeded (left the well) or failed (slid back to the bottom) and on what day. Format the output exactly as shown in the example.

 

Sample Input

6 3 1 1010 2 1 5050 5 3 1450 6 4 150 6 3 11 1 1 10 0 0 0

 

Sample Output

success on day 3failure on day 4failure on day 7failure on day 68success on day 20failure on day 2

 

 


Miguel A. Revilla 1998-03-10  
#include<stdio.h>#include<string.h>int is_end(double h, double& dis, double n){    if(dis > h) return 1;    else    {        dis -= n;        if(dis < 0) return -1;    }    return 0;}int main(){    int i, j, flag, cnt;    double h, d, n, f, dis, e;    while(scanf("%lf%lf%lf%lf", &h, &d, &n, &f) != EOF && h)    {        e = d*f/100;        for(flag=0,dis=cnt=0; !flag; cnt++)        {            if(d-e*cnt >= 0) dis += (d - e*cnt);            flag = is_end(h, dis, n);        }        if(flag == 1) printf("success on day %d\n", cnt);        else if(flag == -1) printf("failure on day %d\n", cnt);    }    return 0;}


解题思路:

模拟题,题目的意思题目已经阐述得很清晰,主要讲一只蜗牛爬墙有升有降的问题,结束的情况为:蜗牛因为降的原因最终还是停留在墙角,或者爬到了最顶端,因为爬会产生劳累从而使一天的形成有所降低(这时要注意行程减少到零不会动的情况,而降的情况是特定的)1y


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>