hdu-1049 Climbing Worm

Climbing Worm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23496 Accepted Submission(s): 16071

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

#include<iostream>
using namespace std;
void main() {
    int n;
    int u, d;
    while (cin >> n>>u>>d&&u>d&&n>0) {
        int time=0;
        
        
        while(n>0) {
            n = n - u;
            time++;
            if (n <= 0) { time = time + 1; break; }
            if(n>0) {
                n = n + d;
                time++;
            
            }
        
        
        }
    
        cout << time-1 << endl;
    }

}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,434评论 0 10
  • 2018.年11月月4号 星期.⑥ 晴天 当一个人熬过了最难过的日子,发现更不会委屈求全了,此刻我深以为然了 人生...
    黄晓来阅读 408评论 2 1
  • 本人看书喜欢先看书名,以此来推断书中会是个怎么的故事,虽然大多数时候这种推断并不准确。看此书之前没看过任何相关介绍...
    离火漆漆阅读 458评论 0 0
  • 2018年4月30日 礼拜一 河南 晴天 后来我总算学会了如何去爱,可惜你早已远去消失在人海,后来终于在眼泪中明白...
    海海张阅读 302评论 0 3
  • 难得的星期天又下雨的时候,可是我没有睡过懒觉,和自己。自己不会安排好时间,跟你昨天又去跳舞了,而且还是跳的是小天鹅...
    lygly9阅读 176评论 0 0