Python执行阶段错误,最短时间需求问题

时间:2018-10-30 17:16:38

标签: python algorithm runtime-error

#!/bin/python3

import math
import os
import random
import re
import sys

class Fraction:

    def __init__(self, numerator, denominator):
        self.numerator = numerator
        self.denominator = denominator

    def return_whole(self):
        return self.numerator // self.denominator


def function1(machines, goal, try_day):
    day = try_day

    tmp_sum = 0

    for element in machines:
        fraction = Fraction(1 * day, element)
        whole = fraction.return_whole()
        tmp_sum += whole

    if tmp_sum >= goal:
        return day

    return False


def recursion_my(machines, goal, lst):
    length = len(lst)

    if length >= 3:
        if not function1(machines, goal, lst[length // 2 - 1]) and function1(machines, goal,lst[length // 2 + 1]):
            if function1(machines, goal, lst[length // 2]):
                return lst[length // 2]

            return lst[length // 2 + 1]

        if function1(machines, goal, lst[length // 2 - 1]):
            return recursion_my(machines, goal, lst[: length // 2])

        else:
            return recursion_my(machines, goal, lst[length // 2:])
    else:
        for i in range(length):
            if function1(machines, goal, lst[i]):
                return lst[i]


def min_time(machines, goal):

    day = min(machines) * goal
    days = []

    for i in range(0, day + 1):
        days.append(i)

    return recursion_my(machines, goal, days)


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    nGoal = input().split()

    n = int(nGoal[0])

    goal = int(nGoal[1])

    machines = list(map(int, input().rstrip().split()))

    ans = min_time(machines, goal)

    fptr.write(str(ans) + '\n')

    fptr.close()

所以我有一个直截了当的问题:我正在解决https://www.hackerrank.com/contests/tsu-advanced-course-in-algorithms-homework-6/challenges/minimum-time-required/copy-from/1311175267这个问题,在一些测试用例(5、7、8、9、10、12)上,我遇到了 Runtime 错误。我知道这里有一个明显的错误,但是请帮助我理解它。我想代码编写起来可能更简单,更简洁,但是首先,我想知道我做错了什么,然后我也很高兴看到您的想法。

0 个答案:

没有答案