【问题标题】:Python algorithm issue [closed]Python算法问题[关闭]
【发布时间】:2017-10-11 10:39:58
【问题描述】:

我的练习算法有问题,不知道它在 python 上是如何工作的。所以这里是:

编写程序,您可以在其中找到一个人可以在邮局等待的最坏情况下的时间 那个,他会在同一天收到他的包裹。他不会早于 08:00 去邮局。

输入: 在标准输入的第一行有一个整数 N - 那是邮政工人的工作间隔数。

在接下来的 N 行中,以 HH:MM HH:MM 格式写入每个工作间隔的开始和结束。 间隔按时间排序,并且不会早于 08:00 开始。在相邻间隔之间有 1 分钟的暂停。 在最后一行有一个整数 K - 这是搜索包的时间(以分钟为单位)。

输出:

在标准输出的一行中,程序应该打印最坏的情况 该人在邮局等待的时间 - 进入办公室的时间和获取包裹的时间 两次都应以 HH:MM 格式打印,并以一个间隔(空格)分隔。 如果有多个最坏的情况,则打印该人到达邮局较早的那个。

范围 1 ≤ N ≤ 20

示例: 输入
3 08:00 08:01 08:02 08:03 08:04 08:05 1

输出
08:01 08:03

提前致谢!

编辑:问题分析 - 练习是技术性的,程序实现应该运行准确。 您应该以分钟为单位进行每次 - 小时*60+分钟。然后每间隔 将设置几个数字,其中 t1 是间隔的开始 分钟,t2 – 间隔结束。 其中 c – 该人在邮局的到达时间。 开始时 c = 480(小时为 08:00)。我们将按行查看间隔。与我 - 下一个工作间隔时间为 t1 到 t2。如果 t2 − t1 ≥ K,那么 Person 将得到 他的包裹在 t1 + K 时刻,他正在等待 t1 + K – с 分钟。将其与 暂时找到一个,如果该选项更好,我们将其保存。然后我们申请 c = t2 - K + 1,然后我们以同样的方式制作间隔。

【问题讨论】:

  • 我无法理解您对输出的期望?
  • 你能提供更多的测试用例吗?另外,至少解释一个!
  • 作为输出 - 进入办公室的时间和拿到包裹的时间
  • “我需要了解它是如何工作的” => 你编写了代码但不明白它是如何工作的???这确实是个问题……
  • 好吧,python 中的“for”与 c++ 不同,我实际上认为我在 python 中写的并不正确

标签: python algorithm


【解决方案1】:

我认为,用户必须给出进入的时间。所以,我将它添加到输入中。

这是我的代码:

# Translates HH:MM to minutes as integers. For example 08:00 => 480
def str2minutes(s):
    sp = s.split(":")
    return int(sp[0]) * 60 + int(sp[1])

# Translates minutes to HH:MM. For example 490 => 08:10
def minutes2str(m):
    hour = int(m / 60)
    minute = m - hour * 60
    if hour < 10: 
        hour = "0" + str(hour)
    if minute < 10: 
        minute = "0" + str(minute)
    return str(hour) + ":" + str(minute)

# Get interval count(N)
interval_count = int(input("Enter work intervals N : "))

# Get intervals separately
interval_list = []
for i in range(interval_count):
    interval_list.append(input("Enter hours and minutes : "))

# Get searching time(K)
searching_time = int(input("Enter the time in minutes for searching the package K : "))

# I think, getting time of entering is necessary
time_of_entering = input("Enter the time of entering : ")

# Control all intervals until find the proper one
for interval in interval_list:
    start =  str2minutes(interval.split()[0])
    finish = str2minutes(interval.split()[1])
    if start >= str2minutes(time_of_entering) and (finish - start) >= searching_time:
        print("Time of entering : " + time_of_entering)
        print("Time of starting to search : " + minutes2str(start))
        print("Time of getting the package : " + minutes2str(start + searching_time))
        break # If we found the proper one, loop should end.

输入:

Enter work intervals N : 4
Enter hours and minutes : 08:00 08:01
Enter hours and minutes : 08:10 08:12
Enter hours and minutes : 08:16 08:20
Enter hours and minutes : 08:24 08:30
Enter the time in minutes for searching the package K : 3
Enter the time of entering : 08:01

输出:

Time of entering : 08:01
Time of starting to search : 08:16
Time of getting the package : 08:19

也许这不是您想要的,但我希望这会有所帮助。

【讨论】:

  • 由于某种原因我没有得到输出:
  • 你能再试一次吗,我编辑了
  • 是的,现在可以了!也感谢 cmets!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 2016-06-28
  • 2020-03-24
相关资源
最近更新 更多