C语言实现水桶灌水问题:保持灌满状态所需时间
#include <stdio.h>
int main() { int capacity = 10; // 水桶的容量为 10L int flow_rate = 3; // 流水的速度为 3L/min int fill_rate = 0; // 初始灌水速度为 0L/min int time = 0; // 初始时间为 0分钟 int water_level = 0; // 初始水位为 0L
while (water_level < capacity) { // 当水位小于容量时循环执行
fill_rate++; // 灌水速度逐渐增加
water_level += fill_rate; // 更新水位
water_level -= flow_rate; // 流水使水位下降
time++; // 时间逐渐增加
}
printf("在 %d 分钟之后,水桶里能保持灌满水的状态。\n", time);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pl7a 著作权归作者所有。请勿转载和采集!