酷代码 AI
菜单

巨龙持续向超硬盾喷射火焰,火焰热量从1开始,每次+1上升,到10结束。巨龙每n次喷火就会哑火一次,也就是说巨龙会在喷火次数是n的倍数时哑火,哑火时的火焰没有热量。超硬盾的初始热量为0,可以累计承受15的热量。请你编写一个程序,在超硬盾承受的总热量第一次超过15的时候,输出总热量。 输入: 一个整数n,表示巨龙会在喷火次数是n的倍数时哑火。 输出: 一个整数,表示超硬盾承受的总热量第一次超过15的时候,当时的总热量。C++

```cpp #include <iostream> using namespace std; int main() { int n; cin >> n; int totalHeat = 0; int fireCount = 0; int heatValue = 1; while (totalHeat <= 15) { fireCount++; if (fireCount % n != 0) { totalHeat += heatValue; } heatValue++; if (heatValue > 10) { heatValue = 1; } } cout << totalHeat << endl; return 0; } ``` 代码解释 1. 输入处理:使用`cin`读取整数`n`,它代表巨龙在喷火次数是`n`的倍数时会哑火。 2. 变量初始化: - `totalHeat`:用于记录超硬盾承受的总热量,初始值为 0。 - `fireCount`:用于记录巨龙喷火的次数,初始值为 0。 - `heatValue`:用于记录每次喷火的火焰热量,初始值为 1。 3. 循环模拟喷火过程: - 使用`while`循环,只要`totalHeat`不超过 15,就继续模拟喷火。 - 每次循环`fireCount`加 1,表示喷火次数增加。 - 判断`fireCount`是否为`n`的倍数,如果不是,则将`heatValue`累加到`totalHeat`中。 - `heatValue`每次循环加 1,当`heatValue`超过 10 时,将其重置为 1。 4. 输出结果:当`totalHeat`第一次超过 15 时,循环结束,使用`cout`输出`totalHeat`的值。 [2025-11-22 19:52:43 | AI问答 | 413点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多