def solve_problem(m, n, cards): flag = False num = 0 nums = set() for i in range(n): num = (num + cards[i]) % m # num += cards[i] #if num % m in nums: if num in nums: flag = True break nums.add(num) if flag: return 1 else: return 0 result = [] while True: try: input_value = input() if not input_value: for j in result: print(j) break else: n, m = map(int, input().split()) cards = list(map(int, input().split())) res = solve_problem(m, n, cards) result.append(res) except: break 以上代码哪里有问题,为啥没输出
时间: 2024-02-10 09:27:10 浏览: 70
以上代码中的问题在于输入的部分逻辑有误。在每次循环中,应该读取一行输入并解析为n和m的值,然后再读取一行输入并解析为cards列表。修改后的代码如下:
```python
def solve_problem(m, n, cards):
flag = False
num = 0
nums = set()
for i in range(n):
num = (num + cards[i]) % m
if num in nums:
flag = True
break
nums.add(num)
if flag:
return 1
else:
return 0
result = []
while True:
try:
input_value = input()
if not input_value:
for j in result:
print(j)
break
else:
n, m = map(int, input_value.split())
cards = list(map(int, input().split()))
res = solve_problem(m, n, cards)
result.append(res)
except:
break
```
这样修改后的代码应该能正确输出结果。请确保在运行代码之前已经输入了相关的测试数据。
阅读全文