由于笔者Python能力有限,观点可能较为片面
在享受Python带来的便捷性的同时,我们发现Python无法像C++一样按照空格或者回车单独读入数据,而只能按照行读入。所以当我们遇到一些不够严谨的数据时,Python无法像C++一样正常读入数据。
- example:
看一个80分代码:
n = int(input())
f = [[0 for i in range(200)] for j in range(200)]
for i in range(n) :
tmp = input().split()
for j in range(n):
f[i][j] = -f[i - 1][j - 1] + f[i][j - 1] + f[i - 1][j] + int(tmp[j])
maxn = 0
for i in range(n):
for j in range(i, n):
maxtt = 0
for k in range(n):
tmp = f[j][k] - f[i - 1][k] - f[j][k - 1] + f[i - 1][k - 1]
maxtt += tmp
if maxtt > maxn: maxn = maxtt
if maxtt < 0: maxtt = 0
print(maxn)
第五个数据点,数据没有按照 n 行 n 列输入,而这会导致这个代码少读很多行。
所以需要将读入部分作一个小修改,下面是我的代码。
n = int(input())
f = [[0 for i in range(200)] for j in range(200)]
mp = [[0 for i in range(200)] for j in range(200)]
cnt = 0
while(True):
try:
tmp = input().split()
for i in tmp:
mp[cnt // n][cnt % n] = int(i)
cnt += 1
except:
break
for i in range(n) :
for j in range(n):
f[i][j] = -f[i - 1][j - 1] + f[i][j - 1] + f[i - 1][j] + mp[i][j]
maxn = 0
for i in range(n):
for j in range(i, n):
maxtt = 0
for k in range(n):
tmp = f[j][k] - f[i - 1][k] - f[j][k - 1] + f[i - 1][k - 1]
maxtt += tmp
if maxtt > maxn: maxn = maxtt
if maxtt < 0: maxtt = 0
print(maxn)
正文完