‘问题: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.
小虫怕井:
小虫有足够的的力量能从井底爬处井面,每分钟爬u英寸,然后必须要休息1分钟,在休息的时候就会下滑d英寸。重复这个过程,他需要多久才能让爬出这个井。
source:East Central North America 2002
分析:在距离井面一段时间时,每分钟爬u英寸,然后每过一分钟就掉d英寸,即每两分钟爬u-d英寸,当最后离井面还剩少于或等于u英寸时,只需爬到井面即可,不会再掉下来。
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int x,y,z;
int a[100],w=0;
while(cin>>x>>y>>z)
{
if(x==0)
{
break;
}
int i;
int b=y-z;
for( i=1;;i++)
{
if(x==y)
{
a[w]=1;
w++;
break;
}
int k;
k=x-b*i;
if(k-y<=0)
{
a[w]=2*i+1;
w++;
break;
}
}
}
for(int i=0;i<w;i++)
{
cout<<a[i]<<endl;
}
}