【hdu - 1049 Climbing Worm】

Climbing Worm

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


Problem Description
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
 

 

Source
 
 
 1 // Project name : 1049 ( Climbing Worm ) 
 2 // File name    : main.cpp
 3 // Author       : Izumu
 4 // Date & Time  : Mon Jul  9 18:50:46 2012
 5 
 6 
 7 #include <iostream>
 8 using namespace std;
 9 
10 const bool goup   = true;
11 const bool godown = false;
12 
13 void CCalClimbingTime(int depth, int up, int down)
14 {
15     // start cal climbing time
16     int time = 0;
17     if (depth == 0)
18     {
19         time = 1;
20     }
21     else
22     {
23         bool go = goup;
24         while (depth > 0)
25         {
26             if (go == goup)
27             {
28                 depth -= up;
29                 go = godown;
30                 time++;
31             }
32             else
33             {
34                 depth += down;
35                 go = goup;
36                 time++;
37             }
38         }
39     }
40 
41     cout << time << endl;
42 }
43 int main()
44 {
45     int depth, up, down;
46     while (cin >> depth >> up >> down && depth + up + down)
47     {
48         CCalClimbingTime(depth, up, down);
49     }
50     return 0;
51 }
52 
53 // end 
54 // ism 

 

posted @ 2012-07-09 19:10  ismdeep  阅读(163)  评论(0编辑  收藏  举报