有一口井,77米深,井底有只青蛙,白天往上爬7米,夜里下落5米,问多少天爬出井口,请给出C语言代码
时间: 2024-05-13 21:20:57 浏览: 18
```c
#include <stdio.h>
int main() {
int depth = 77; // 井深
int day = 7; // 白天爬升距离
int night = 5; // 夜晚下落距离
int height = 0; // 当前高度
int days = 0; // 爬出井口的天数
while (height < depth) {
height += day;
days++;
if (height >= depth) {
break;
}
height -= night;
}
printf("爬出井口需要%d天\n", days);
return 0;
}
```
相关问题
有一口井深30米,井底有一只青蛙,这个青蛙每天可以往上爬三米,但是会往下滑2米,计算多少天可以爬出井。代码怎么写
题目很有意思,我来回答你的问题。根据题目的描述,青蛙每天可以向上爬3米,但会往下滑2米,所以每天实际上是能爬1米的。因此,爬出30米深的井需要30天。
以下是Python实现的代码:
```python
height = 30
up = 3
down = 2
daily_progress = up - down
days = (height - up) // daily_progress + 1
print("青蛙需要", days, "天才能爬出井。")
```
运行结果为:
```
青蛙需要 30 天才能爬出井。
```
注意,这里用到了整除符号 `//`,因为题目要求的是天数,所以最后需要加上1。
c++有一口井深10米有一只蜗牛白天爬2米夜晚滑1米几天出牛
根据题目所给的信息,蜗牛白天爬2米夜晚滑1米,井深10米,我们可以计算蜗牛需要多少天才能从井底爬出来。
在白天,蜗牛每天能往上爬2米,所以白天需要爬5天才能到达井口(10米÷2米/天=5天)。
但是,在夜晚,蜗牛会滑下1米,所以在第5天夜晚的时候,蜗牛会滑下2米,只剩下3米的高度。在第6天的白天,蜗牛能够再爬2米,但是还剩下1米的高度,所以蜗牛无法再爬出井口。
因此,根据题目给出的条件,蜗牛需要5天才能从井底爬出来。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)