韩信点兵典故Python代码
时间: 2023-11-05 21:51:57 浏览: 101
韩信点兵python程序代码
5星 · 资源好评率100%
以下是一个简单的 Python 代码,用于实现韩信点兵典故:
```python
def calculate_soldiers(total_count, remainder_list):
possible_counts = []
for i in range(1, total_count+1):
valid = True
for j in remainder_list:
if i % (total_count+1) != j:
valid = False
break
if valid:
possible_counts.append(i)
return possible_counts
total_count = int(input("请输入总人数:"))
remainder_list = list(map(int, input("请输入余数列表,以空格分隔:").split()))
possible_counts = calculate_soldiers(total_count, remainder_list)
if len(possible_counts) == 0:
print("无法确定士兵总数!")
elif len(possible_counts) > 1:
print("有多种可能的士兵总数:", possible_counts)
else:
print("确定的士兵总数为:", possible_counts[0])
```
输入示例:
```
请输入总人数:10
请输入余数列表,以空格分隔:3 4 5
```
输出示例:
```
确定的士兵总数为:35
```
阅读全文