题目链接
一个毛毛虫在n英尺深的井里,它每分钟向上爬u英尺,然后休息1分钟,在休息的过程中向下滑落d英尺,重复上述过程,求爬出井共需要多久。
向上爬的高度大于或等于井的高度时候的时间即为所求。
改进后
#include<stdio.h> #define MAX 102 int main(){ //freopen("TXC.txt","r",stdin); int n,u,d; while(~scanf("%d%d%d",&n,&u,&d),n){ int t=0;//爬的时间 int s=0;//向上爬的英尺数 for(int i=0;i<MAX;i++){ s+=u;//向上爬 t++; if(s>=n){//向上与井的高度比较 printf("%d\n",t); break; } else{ s-=d;//向下落 t++; } } } return 0; }