首页 > 解决方案 > 针对 USACO 问题的 Python 代码中的 EOF 错误重新植被

问题描述

我一直在研究 USACO 问题,到目前为止我有这个代码:

fin = open('revegetate.in', 'r')
fout = open('revegetate.out', 'w')

pasture_num, cow_num = map(int, input().split())
pastures = []
ans = [1] * pasture_num

for _ in range(cow_num):
    past1, past2 = sorted(map(int, input().split()))
    pastures.append([past1 - 1, past2 - 1])

pastures.sort()
for a, b in pastures:
    if ans[a] == ans[b]:
        ans[b] += 1

        for i, j in pastures[:pastures.index([a, b])]:
            if ans[i] == ans[j]:
                ans[b] += 1
print(''.join(map(str, and)))

虽然当我运行它时,它会在第 4 行返回 EOF 错误:

pasture_num, cow_num = map(int, input().split())

样本输入将是:

5 6
4 1
4 2
4 3
2 5
1 2
1 5

如何修复此 EOF 错误?

标签: pythonpython-3.x

解决方案


我通过将其更改为 fin.readline() 而不是 input() 来修复它。


推荐阅读