【问题标题】:Codechef - NZEC Error in python codeCodechef - python 代码中的 NZEC 错误
【发布时间】:2015-08-22 09:34:28
【问题描述】:

代码在我的机器上运行良好,但是当我在 codechef 上编译它时,它给出了一个 NZEC(运行时错误)。

问题链接:https://www.codechef.com/problems/PPTEST

关于我的解决方案:我根据每个测试用例的时间和点值计算了它们的百分位数。然后我根据百分位数对每个测试用例中的条目进行了排序。

import sys
def check(x):
    if not(x in range(1,100)):
        sys.exit(1)
T = input()
check(T)

N_W = []
C_P_T = {}
tp = []
tt = []

for i in range(0,T):
    tp.append(0)
    tt.append(0)
    N_W.append(map(int, raw_input().split())) 

    check(N_W[i][0])
    check(N_W[i][1]) 

    C_P_T[i] = []
    for j in range(0,N_W[i][0]):
        C_P_T[i].append(map(int, raw_input().split()))

        check(C_P_T[i][j][0])
        check(C_P_T[i][j][1])
        check(C_P_T[i][j][2])

        C_P_T[i][j].append(N_W[i][1]-C_P_T[i][j][2])
        C_P_T[i][j].append(C_P_T[i][j][1]*C_P_T[i][j][0])
        C_P_T[i][j].pop(0)
        C_P_T[i][j].pop(0)
        C_P_T[i][j].pop(0)
        tp[i]+= C_P_T[i][j][1]
        tt[i]+=C_P_T[i][j][0]


for i in range(0,T):
    C_P_T[i].sort(key = lambda x : x[0] , reverse = True)
    item_time = C_P_T[i][0][0]
    percentile_time = (C_P_T[i][0][0]/float(tt[i]))*((len(C_P_T[i])-1)/float(len(C_P_T[i])))

    for j in range(0,N_W[i][0]):
        if C_P_T[i][j][0] == item_time:
            C_P_T[i][j].append(percentile_time)
        else:
            item_time = C_P_T[i][j][0]
            percentile_time = (C_P_T[i][j][0]/float(tt[i]))*((len(C_P_T[i])-j-1)/float(len(C_P_T[i])))
            C_P_T[i][j].append(percentile_time)

for i in range(0,T):
    C_P_T[i].sort(key = lambda x : x[1] , reverse = True)
    item_points = C_P_T[i][0][1]
    percentile_points = (C_P_T[i][0][1]/float(tp[i]))*((len(C_P_T[i])-1)/float(len(C_P_T[i])))

    for j in range(0,N_W[i][0]):
        if C_P_T[i][j][1] == item_points:
            C_P_T[i][j].append(percentile_points)
        else:
            item_points = C_P_T[i][j][1]
            percentile_points = ((C_P_T[i][j][1])/float(tp[i]))*((len(C_P_T[i])-j-1)/float(len(C_P_T[i])))
            C_P_T[i][j].append(percentile_points)

    C_P_T[i][j].append(C_P_T[i][j][2]+C_P_T[i][j][3])
    C_P_T[i][j].append(N_W[i][1]-C_P_T[i][j][0])
    C_P_T[i][j].pop(2)
    C_P_T[i][j].pop(2)

    C_P_T[i].sort(key = lambda x : x[2],reverse = True)

for i in range(0,T):
    points = 0
    for j in range(0,N_W[i][0]):
        if N_W[i][1]-C_P_T[i][j][3] >= 0:
            points+=C_P_T[i][j][1]
            N_W[i][1]-=C_P_T[i][j][3]
    print points

【问题讨论】:

  • 您能尝试隔离问题吗?

标签: python


【解决方案1】:

NZEC 的意思是“非零退出代码”,因此这可能发生在您的 check() 函数中的 sys.exit(1) 中。您从input() 收到的不是整数或不在正确的范围内。

更新:我注意到您使用range(1, 100) 进行有效性测试。

但是 codechef 的问题描述指出1 ≤ T ≤ 100。相当于range(1, 101)

因此,codechef 可能会向您的代码传递一个完全有效的 100,而您的代码会拒绝它,可能与您看到的确切错误一样。

【讨论】:

  • @Patrick_Maupin 我尝试将 T 转换为整数,但没有成功。我使用的是 python 2.7,当我在 codechef 上编译时,我也选择了 2.7。
猜你喜欢
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多