0

我认为我的算法是正确的(可能做得很糟糕),并且我在 ideone.com 中获得了所需的输出。但在 SPOJ 中,它一直说“运行时错误 NZEC”。然后我添加了“sys.exit(0)”。然后NZEC错误停止了。但现在它显示“错误答案”

请提出一些更改以使其正确。

这是问题的链接:

http://www.spoj.com/problems/RAFANOLE/

这是我的代码:

def main():
    import sys;
    inp=sys.stdin.read().split("\n");
    t=int(inp[0]);
    i=1;
    g_N=dict();
    g_D=dict();
    t_N=dict();
    t_D=dict();
    while(i<t+1):
        a=inp[i].split();
        game_N =0;
        curr_N =0;
        game_D =0;
        curr_D =0;
        tie_N=0;
        tie_D=0;
        k=0;
        n=len(a);
        while ((k<n)and((game_N<6)or(game_D<6))):
            if(a[k]=='N'):
                curr_N = curr_N +1;
                if(curr_N==4 and curr_D < 3):
                    game_N=game_N +1;
                    curr_N=0;
                    curr_D=0;
                if(curr_N==3 and curr_D ==3) :
                    while(a[k]!=a[k+1]):
                        k=k+1;
                    if(a[k]=='N'):
                        game_N=game_N+1;
                        curr_N=0;
                        curr_D=0;
            if(a[k]=='D'):
                curr_D=curr_D+1;
                if(curr_D==4 and curr_N<3):
                  game_D=game_D+1;
                  curr_D=0;
                  curr_N=0;
                if(curr_N==3 and curr_D ==3) :
                   while(a[k]!=a[k+1]):
                       k=k+1;
                   if(a[k]=='D'):
                       game_D=game_D+1;
                       curr_N=0;
                       curr_D=0;
            k=k+1;
        if((game_N==6 and game_D==5 ) or (game_N==5 and game_D==6)):
            curr_N=0;
            curr_D=0;
            while(game_N != 7 and game_D != 7):
                if (a[k]=='N'):
                    curr_N=curr_N+1;
                if(a[k]=='D'):
                    curr_D=curr_D+1;
                if(curr_N==4 and curr_D<3):
                    game_N=game_N+1;
                    curr_D=0;
                    curr_N=0;
                if(curr_D==4 and curr_N<3):
                    game_D=game_D+1;
                    curr_N=0;
                    curr_D=0;
        if (game_N==6 and game_D ==6):
            while (1):
                if(a[k]=='N'):
                    tie_N=tie_N+1;
                if(a[k]=='D'):
                    tie_D=tie_D+1;
                if(((tie_D==7 or tie_N==7) and (abs(tie_D-tie_N)>=2)) or ((tie_D>7 or tie_N>7)and(abs(tie_D-tie_N)>=2))):
                    k=k+1;
                    break;
                k=k+1;

        if(tie_N>tie_D):
            game_N=game_N+1;
        elif(tie_N<tie_D) :
            game_D=game_D+1;
        g_N[i]=game_N;
        g_D[i]=game_D;
        t_N[i]=tie_N;
        t_D[i]=tie_D;

        i=i+1;
    i=1;
    while(i<t+1):        
        if(g_N[i]>g_D[i]):
            if(t_N[i]==0 and t_D[i] ==0):
                print ("N %d" % g_N[i]);
                print ("D %d" % g_D[i]);
                print "\t";
            else :
                print ("N %d(%d-%d)"%(g_N[i],t_N[i],t_D[i]));
                print ("D %d" % g_D[i]);
        else:
            if(t_N[i]==0 and t_D[i] ==0):
                print ("D %d" % g_D[i]);
                print ("N %d" % g_N[i]);
            else :
                print ("D %d(%d-%d)"%(g_D[i],t_D[i],t_N[i]));
                print ("N %d" % g_N[i]);
        i=i+1;
    sys.exit(0)
if __name__=="__main__":
    main();
4

1 回答 1

0

你检查

if((game_N==6 and game_D==5 ) or (game_N==5 and game_D==6)):
        curr_N=0;
        curr_D=0;
        while(game_N != 7 and game_D != 7):

这是错误。if, 的条件可能后跟game_N=6,game_d=D,这在您的场景中不考虑,如果其中任何一个是 6,另一个是 5,则迭代直到任何 1 达到 7,从而使平局条件永远不会发生。

于 2013-11-08T18:41:09.013 回答