題目:
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.
題意: 注意:每次休息時間就要增加1,這個不能忘記 每分鐘怕u,這樣一加有可能超出總長,一定中間加上判斷
#include<stdio.h>
int main()
{
int n,u,d,sum;
while(scanf("%d%d%d",&n,&u,&d),n+u+d)
{
int i;sum=0;
for(i=0;i<=n;)
{
i+=u;
sum++;
if(i>=n) break;
i=i-d,sum++;//休息的時間
}
printf("%d\n",sum);
}
return 0;
}
解題者:楊聰穎
解題時間:2014-9-27
題目描述:有個蟲子,從n深處向上爬,每次爬u就要休息一分鐘,休息這一分鐘會滑下d,求最後爬上來需要多長時間,
解題思路:利用for循環進行循環相加,累計計算
難點:容易忽略休息一分鐘時,每次要加上那一分鐘
關鍵點:for循環的利用
體會:題目不難,但馬虎時容易出錯
備注:中間一次的if判斷不能忘記,否則會出現重複,回出現多加
源代碼
#include<stdio.h>
int main()
{
int n,u,d,sum;
while(scanf("%d%d%d",&n,&u,&d),n+u+d)
{
int i;sum=0;
for(i=0;i<=n;)
{
i+=u;
sum++;
if(i>=n) break;
i=i-d,sum++;//休息的時間
}
printf("%d\n",sum);
}
return 0;
}
代碼詳細注釋