这个python代码中的错误在哪里?

时间:2017-10-01 04:32:44

标签: python-3.x

img

前几个例子:

Input:
 10
 1
 4 5 6
Output:
 6
另一个:

Input:
 10
 2
 3 3 3
 7 7 4
Output:
 4

我把这段代码放在某些情况下是正确的但不是所有问题都在哪里?

n = int(input())
q = int(input())
z = 0
repeat = 0
ans = 0
answ = []
arrx = []
arry = []
for i in range(q):
    maxi = 0
    x,y,w = [int(i) for i in input().split()]
    x,y = x+1, y+1
    if((arrx.count(x)>=1)):
        index = arrx.index(x)
        if(y==arry[index]):
            if(answ[index]==ans):
                repeat += answ[index]
                z = answ[index]
    arrx.append(x)
    arry.append(y)
    if((w>x or w>y) or (w>(n-x) or w>(n-y))):
        maxi = max(x, y, (n-x), (n-y))
    if(((x>=w) or (y>=w)) or (((n-x)>=w) or ((n-y)>=w))):
        maxi = w
    ans = max(ans, maxi)
    answ.append(ans)
    if(ans>z):
        repeat = 0
print(ans+repeat)

1 个答案:

答案 0 :(得分:0)

我在代码中看到的问题是,当问题需要二维数组时,您将数据作为两个一维数组arrxarry处理。您应该能够打印出您的数据结构并查看火山的热图。对于第一个例子,你在地图中间有一个热火山:

10
1
4 5 6

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 1]
[2, 3, 3, 3, 3, 3, 3, 3, 2, 1]
[2, 3, 4, 4, 4, 4, 4, 3, 2, 1]
[2, 3, 4, 5, 5, 5, 4, 3, 2, 1]
[2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
[2, 3, 4, 5, 5, 5, 4, 3, 2, 1]
[2, 3, 4, 4, 4, 4, 4, 3, 2, 1]
[2, 3, 3, 3, 3, 3, 3, 3, 2, 1]
[2, 2, 2, 2, 2, 2, 2, 2, 2, 1]

最热的(6)点显然是一座火山本身。对于第二个例子,你有两个较冷的火山:

10
2
3 3 3
3 3 3
7 7 4

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 0, 0, 0, 0]
[0, 1, 2, 2, 2, 1, 0, 0, 0, 0]
[0, 1, 2, 3, 2, 1, 0, 0, 0, 0]
[0, 1, 2, 2, 3, 2, 1, 1, 1, 1]
[0, 1, 1, 1, 2, 3, 2, 2, 2, 2]
[0, 0, 0, 0, 1, 2, 3, 3, 3, 2]
[0, 0, 0, 0, 1, 2, 3, 4, 3, 2]
[0, 0, 0, 0, 1, 2, 3, 3, 3, 2]
[0, 0, 0, 0, 1, 2, 2, 2, 2, 2]

热点可能是两个火山中较热的一个,也可能是某个位于两个火山的重叠部位的地方。在这种情况下,重叠点不会比最热的(4)火山更热。但如果火山更近,一个或多个火山可能有。