韩信点兵python程序代码
时间: 2023-10-08 21:14:18 浏览: 151
以下是韩信点兵的 Python 代码:
```python
# 输入总人数和报数的数字
total_num = int(input("请输入总人数:"))
report_num = int(input("请输入报数的数字:"))
# 初始话士兵列表,全部设置为 True
soldiers = [True] * total_num
# 当剩余人数大于 1 时,执行点兵过程
while soldiers.count(True) > 1:
# 初始化当前报数和当前士兵编号
current_report = 0
current_soldier = 0
# 找到第一个未被淘汰的士兵
while not soldiers[current_soldier]:
current_soldier += 1
# 依次报数,淘汰报数为报数数字的士兵
for i in range(total_num):
if soldiers[i]:
current_report += 1
if current_report == report_num:
soldiers[i] = False
current_report = 0
# 找到下一个未被淘汰的士兵
while not soldiers[current_soldier]:
current_soldier += 1
# 输出本轮淘汰的士兵编号
print("淘汰的士兵编号为:", current_soldier + 1)
# 输出剩余的最后一名士兵编号
print("剩余的最后一名士兵编号为:", soldiers.index(True) + 1)
```
使用方法:
1. 运行程序后,输入总人数和报数的数字。
2. 程序会输出每一轮淘汰的士兵编号,最后输出剩余的最后一名士兵编号。
相关推荐
![](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)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)