题目描述 有一口深度为 high 米的水井,井底有一只青蛙,它每天白天能够沿井壁向上爬 up 米,夜里则顺井壁向下滑 down 米,若青蛙从某个早晨开始向外爬,对于任意指定的 high、up 和 down 值(均为自然数),计算青蛙多少天能够爬出井口? 本题输入high,up,down; 本题需注意不能将up和down进行做差然后去判断,要考虑当up大于等于激距离洞口的距离时,青蛙直接出去了,就不用再down了。 详见代码: #include #include #include #define endl '\n' #define buff ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr); using namespace std; int main() {
ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int high,up,down;
int sum=0,t=0;
cin>>high>>up>>down;
while(sum<high)
{
sum+=up;
t++;
if(sum<high)
sum-=down;
if(sum>=high)
{break;
}
}
cout<<t<<'\n';
return 0; }