Python 解决方案适用于样本数据,但在线判断给出错误?

Python 解决方案适用于样本数据,但在线判断给出错误?,python,python-2.7,Python,Python 2.7,这就是我试图解决的问题: 有N(1)≤ N≤ 4) 佛森守护着一个珍贵的宝藏, 你很想得到它。问题是,狐狸 当然,他们不会允许这样做——至少,在他们离开的时候不会 醒着 幸运的是,通过仔细观察,你已经看到每只狐狸 有规律的睡眠周期。特别是,ith福克斯保持清醒长达一个小时 Ai(1)≤ 人工智能≤ 23)小时,然后睡Si(1≤ Si≤ 23)小时, 无限期重复此模式(2≤ Ai+Si≤ 24). 一开始 在你的夺宝尝试中,ith狐狸是 完全Oi(0≤ OiAi+Si)在其周期中的小时数 有T(

这就是我试图解决的问题:

有N(1)≤ <代码>N≤ 4) 佛森守护着一个珍贵的宝藏, 你很想得到它。问题是,狐狸 当然,他们不会允许这样做——至少,在他们离开的时候不会 醒着

幸运的是,通过仔细观察,你已经看到每只狐狸 有规律的睡眠周期。特别是,
i
th福克斯保持清醒长达一个小时
Ai
(1)≤ <代码>人工智能≤ 23)小时,然后睡
Si
(1≤ <代码>Si≤ 23)小时, 无限期重复此模式(2≤ <代码>Ai+Si≤ 24). 一开始 在你的夺宝尝试中,
i
th狐狸是 完全
Oi
(0≤ <代码>OiAi+Si)在其周期中的小时数

T
(1≤ <代码>T≤ 20) 如上所述的场景。每一个,, 你想确定所有的狐狸会在多长时间内消失 同时睡着,让你抓住他们的宝藏,或者如果这 永远不会发生

输入

Line 1: 1 integer, T
For each scenario:
    Line 1: 1 integer, N
    Next N lines: 3 integers, Ai, Si, and Oi, for i = 1..N
For each scenario:
    Line 1: 1 integer, the minimum number of hours after the start to 
     wait until all of the Foxen are asleep during the same hour. If this
     will never happen, output the string "Foxen are too powerful" (without
     quotes) instead.
2
2
2 1 2
2 2 1
3
1 1 0
1 1 0
1 1 1
6
Foxen are too powerful
输出

Line 1: 1 integer, T
For each scenario:
    Line 1: 1 integer, N
    Next N lines: 3 integers, Ai, Si, and Oi, for i = 1..N
For each scenario:
    Line 1: 1 integer, the minimum number of hours after the start to 
     wait until all of the Foxen are asleep during the same hour. If this
     will never happen, output the string "Foxen are too powerful" (without
     quotes) instead.
2
2
2 1 2
2 2 1
3
1 1 0
1 1 0
1 1 1
6
Foxen are too powerful
样本输入

Line 1: 1 integer, T
For each scenario:
    Line 1: 1 integer, N
    Next N lines: 3 integers, Ai, Si, and Oi, for i = 1..N
For each scenario:
    Line 1: 1 integer, the minimum number of hours after the start to 
     wait until all of the Foxen are asleep during the same hour. If this
     will never happen, output the string "Foxen are too powerful" (without
     quotes) instead.
2
2
2 1 2
2 2 1
3
1 1 0
1 1 0
1 1 1
6
Foxen are too powerful
样本输出

Line 1: 1 integer, T
For each scenario:
    Line 1: 1 integer, N
    Next N lines: 3 integers, Ai, Si, and Oi, for i = 1..N
For each scenario:
    Line 1: 1 integer, the minimum number of hours after the start to 
     wait until all of the Foxen are asleep during the same hour. If this
     will never happen, output the string "Foxen are too powerful" (without
     quotes) instead.
2
2
2 1 2
2 2 1
3
1 1 0
1 1 0
1 1 1
6
Foxen are too powerful
当我输入给定的示例案例并获得预期的输出时,我的解决方案按预期工作。但当我将代码提交给在线法官时,它给出了裁剪错误。现在没有错误的细节,这使得很难找到问题所在

以下是我迄今为止一直在研究的解决方案:

# ai is awake hours
# si is sleep hours.
# ai + si <= 24.

# False == sleep. True == awake.

datasets = int(raw_input());
foxen = [];
number_of_foxen = 0;
foxes = [];

class fox:
    def __init__(self, a, s, i):
        self.awake = a;
        self.sleep = s;
        self.current = i;
    awake = 0;
    sleep = 0;
    current = 0;

    def next(self):
        if ( self.sleep + self.awake-1 > self.current ) :
            self.current = self.current+1;
        else:
            self.current = 0;
        return self.current;

    def check(self):
        if(self.current>=self.awake):
            return False;
        return True;

    def printdata(self):
        print "awake="+str(self.awake)+" sleep="+str(self.sleep)+" current="+str(self.current);
        #return "awake="+str(self.awake)+" sleep="+str(self.sleep)+" current="+str(self.current);


for i in range(0, datasets):
    number_of_foxen = int(raw_input());

    for j in range(0, number_of_foxen):
        foxen.append(raw_input());
        x = foxen[j].split();
        a = fox(int(x[0]), int(x[1]), int(x[2]));
        foxes.append(a);

    solution = False;
    for j in range(0, 48):
        #print "hour number = " + str(j);

        #for k in range(0, len(foxes)):
            #print "fox number="+ str(k)+" "+ foxes[k].printdata()+str(foxes[k].check());

        count = 0 ;
        for k in range(0, len(foxes)):
            if(foxes[k].check()==False):
                count+=1;
                #print "count = "+str(count);
                #print len(foxes);
            if( (int(count) == int(len(foxes))) and (solution == False)  ):
                #print "this runs now *************";
                solution = True;
                number = j;

        for k in range(0, len(foxes)):
            foxes[k].next();

    if(solution==True):
        print number;
    else:
        print "Foxen are too powerful";

    #print "Foxen are too powerful";
    foxen = [];
    number_of_foxen = 0;
    foxes = [];
#人工智能是醒着的
#si是睡眠时间。
#ai+si自电流):
自电流=自电流+1;
其他:
自电流=0;
返回自电流;
def检查(自我):
如果(self.current>=self.awake):
返回False;
返回True;
def打印数据(自身):
打印“awake=“+str(self.awake)+”sleep=“+str(self.sleep)+”current=“+str(self.current));
#返回“awake=“+str(self.awake)+”sleep=“+str(self.sleep)+”current=“+str(self.current));
对于范围内的i(0,数据集):
_foxen的数量=int(原始输入());
对于范围内的j(0,x的个数):
append(原始输入());
x=foxen[j].split();
a=fox(int(x[0])、int(x[1])、int(x[2]);
附加(a);
解=假;
对于范围(0,48)内的j:
#打印“小时数=”+str(j);
#对于范围(0,len(狐狸))中的k:
#打印“fox number=“+str(k)+”“+foxes[k].printdata()+str(foxes[k].check());
计数=0;
对于范围(0,len(狐狸))中的k:
如果(foxes[k].check()==False):
计数+=1;
#打印“计数=”+str(计数);
#列印(狐狸);
如果((int(count)=int(len(foxes)))和(solution==False)):
#打印“现在运行**********”;
解=真;
数字=j;
对于范围(0,len(狐狸))中的k:
狐狸[k].next();
如果(解决方案==True):
打印号码;
其他:
打印“狐狸太强大了”;
#打印“狐狸太强大了”;
foxen=[];
_-foxen的数量=0;
狐狸=[];

Jorge的评论是正确的,您的算法似乎没有任何问题,只是任意的48小时截止时间

然而:

1) 您的print语句没有使用Python3+的正确语法。例如,您的最终打印语句
print“Foxen太强大了”必须更改为在Python 3中工作,请尝试使用
print('Foxen太强大了')

2) 我也看到一些奇怪的C/MatLab语法,行以分号结尾,if语句中的条件用双括号括起来。这可能不是问题,但取决于提交答案的系统有多挑剔,您可能需要稍微清理一下

3) 一定要增加搜索的截止时间。我推荐一个相当大的值,大约10000小时,只是为了确保它不会成为一个因素

我已经自由地进行了上述所有更改,因此我现在发布生成的代码:

# ai is awake hours
# si is sleep hours.
# ai + si <= 24.

# False == sleep. True == awake.

datasets = int(raw_input())
foxen = []
number_of_foxen = 0
foxes = []

class fox:
    def __init__(self, a, s, i):
        self.awake = a
        self.sleep = s
        self.current = i
    awake = 0
    sleep = 0
    current = 0

    def next(self):
        if ( self.sleep + self.awake-1 > self.current ): 
            self.current = self.current+1
        else:
            self.current = 0
        return self.current

    def check(self):
        if(self.current>=self.awake):
            return False
        return True

    def printdata(self):
        print ("awake="+str(self.awake)+" sleep="+str(self.sleep)+"     current="+str(self.current))
        #return ("awake="+str(self.awake)+" sleep="+str(self.sleep)+" current="+str(self.current))


for i in range(0, datasets):
    number_of_foxen = int(raw_input())

    for j in range(0, number_of_foxen):
        foxen.append(raw_input())
        x = foxen[j].split()
        a = fox(int(x[0]), int(x[1]), int(x[2]))
        foxes.append(a)

    solution = False
    for j in range(0, 10000):
        #print ("hour number = " + str(j))

        #for k in range(0, len(foxes)):
            #print ("fox number="+ str(k)+" "+ foxes[k].printdata()+str(foxes[k].check()))

        count = 0 
        for k in range(0, len(foxes)):
            if(foxes[k].check()==False):
                count+=1
                #print ("count = "+str(count))
                #print (len(foxes))
            if (int(count) == int(len(foxes)) and (solution == False)):
                #print ("this runs now *************")
                solution = True
                number = j

        for k in range(0, len(foxes)):
            foxes[k].next()

    if(solution == True):
        print (number)
    else:
        print ("Foxen are too powerful")

    #print ("Foxen are too powerful")
    foxen = []
    number_of_foxen = 0
    foxes = []
#人工智能是醒着的
#si是睡眠时间。
#ai+si自电流):
自电流=自电流+1
其他:
自身电流=0
返回自电流
def检查(自我):
如果(self.current>=self.awake):
返回错误
返回真值
def打印数据(自身):
打印(“awake=“+str(self.awake)+”sleep=“+str(self.sleep)+”current=“+str(self.current))
#return(“awake=“+str(self.awake)+”sleep=“+str(self.sleep)+”current=“+str(self.current))
对于范围内的i(0,数据集):
_foxen的数量=int(原始输入()
对于范围内的j(0,x的个数):
append(原始输入())
x=foxen[j].split()
a=fox(int(x[0])、int(x[1])、int(x[2]))
狐狸。附加(a)
解决方案=错误
对于范围(0,10000)内的j:
#打印(“小时数=“+str(j))
#对于范围(0,len(狐狸))中的k:
#打印(“fox number=“+str(k)+”“+foxes[k].printdata()+str(foxes[k].check()))
计数=0
对于范围(0,len(狐狸))中的k:
如果(foxes[k].check()==False):
计数+=1
#打印(“计数=”+str(计数))
#印刷品(蓝(狐狸))
如果(int(count)==int(len(foxes))和(solution==False)):
#打印(“现在运行**********”)
解决方案=真
数字=j
对于范围(0,len(狐狸))中的k:
狐狸[k].下一个()
如果(解决方案==True):
打印(数字)
其他:
打印(“Foxen太强大了”)
#打印(“Foxen太强大了”)
foxen=[]
_foxen的数量=0
狐狸=[]

祝你好运

你的电脑最大的问题是