使用Python,用于将小时分钟时间转换为文本格式的程序
假设我们有两个输入:小时和分钟。 我们必须以文本格式显示时间。 例如 −
- 8:00 : 8点整
- 8:01 : 八点零一
- 8:10 : 八点十分
- 8:15 : 八点一刻
- 8:30 : 八点半
- 8:40 : 八点四十分(离九点还有二十分钟)
- 8:45 : 八点四十五分(差一刻九)
- 8:47 : 八点四十七分(差十三分钟九点)
- 8:28 : 八点二十八分
所以,如果输入为h = 9,m = 42,则输出将为十点前十八分钟
为了解决这个问题,我们将遵循以下步骤−
- text: =包含30个不同数字值的文本列表,如下所示: [“one”, “two”, “three”, “four”, “five”, “six”, “seven”, “eight”,”nine”,”ten”, “eleven”, “twelve”, “thirteen”, “fourteen”, “quarter”, “sixteen”,”seventeen”, “eighteen”, “nineteen”, “twenty”, “twenty-one”,”twenty-two”, “twenty-three”, “twenty-four”, “twenty-five”,”twenty-six”, “twenty-seven”, “twenty-eight”, “twenty-nine”, “half”]
- op: = 空字符串
- 如果m与0相同,则
- op: = text[h – 1] +“点钟”
- 否则,当m和30相同时,则
- op: = text[m – 1] + ‘过去’ + text[h – 1]
- 否则,当m和1相同时,则
- op: = text[m – 1]+ “过去” + text[h – 1] + “分”
- 否则,当m和15相同时,则
- op: = text[m – 1] + ‘过去’ + text[h – 1]
- 否则,当m – 30不为零时,则
- op: = text[m – 1] + ‘过去’ + text[h – 1] + ‘分钟’
- 否则,当m和45相同时,则
- op: =”离” + text[h] + “还有一刻”
- 否则
- op: = text[(60 – m) -1] + ‘离’ + text[h] + “还有” + “分钟”
- 返回op
例子
让我们看一下下面的实现,以更好地理解−
def solve(h, m):
text=["one", "two", "three", "four", "five", "six", "seven", "eight","nine","ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen","seventeen", "eighteen", "nineteen", "twenty", "twenty-one","twenty-two", "twenty-three", "twenty-four", "twentyfive"," twenty-six", "twenty-seven", "twenty-eight","twenty-nine", "half"]
op=""
if (m == 0):
op = text[h - 1] + "点钟"
elif (m == 30):
op = text[m - 1]+ "过去" + text[h - 1]
elif (m == 1):
op = text[m - 1] + "过去" + text[h - 1] + "分"
elif (m == 15):
op = text[m - 1]+ "过去" + text[h - 1]
elif (m < 30):
op = text[m - 1] + "过去" + text[h - 1] + '分钟'
elif (m==45):
op = "离" + text[h] + "还有一刻"
else:
op = text[(60 - m)-1] + '离' + text[h] + "还有" + "分钟"
return op
h = 9
m = 42
print(solve(h, m))
输入
9, 42
输出
十点前十八分钟