用户:
小鱼队长查看:1 回复:4 评论:1 创建时间:2022-02-13T11:36:25
【作品展示】

【作品介绍】
三、编程题【编程实现】计算某个电梯的用电量。电梯可到达最低楼层为地下3层(-3),最高为地上12层(12),中间没有0层;电梯向上运行时每上升1层消耗1单位电量,向下运行时每下降1层消耗0.3 单位电量;请你通过输入的某段时间内电梯停过的楼层顺序,计算电梯消耗了多少单位电量。
输入描述:N个数字(2<=N<=10),数字间以逗号分隔,代表电梯停过的楼层[-3,12];
输出描述:电梯消耗的单位电量数;
【样例输入】
1,11,1
【样例输出】
13.0
【作品源代码】
n = input()
n_list = n.split(',')
int_list = []
喵1 = 0
喵2 = 0
az = True
for i in n_list:
try:
int_list.append(int(i))
except:
print('输入有误,请输入-3到12(0除外)之间的数字,以逗号分隔')
for i in int_list:
if i == 0 or i > 12 or i < -3:
print('输入有误,请输入-3到12(0除外)之间的数字')
az = False
break
if az:
for i in range(len(int_list)-1):
if int_list[i+1] - int_list[i] > 0:
if int_list[i+1] > 0 and int_list[i] > 0:
喵1 += 1*(int_list[i+1] - int_list[i])
elif int_list[i+1] < 0 and int_list[i] < 0:
喵1 += 1*(int_list[i+1] - int_list[i])
else:
喵1 += 1*(int_list[i+1] - int_list[i] - 1)
elif int_list[i+1] - int_list[i] < 0:
if int_list[i+1] > 0 and int_list[i] > 0:
喵2 += 0.3*(int_list[i] - int_list[i+1])
elif int_list[i+1] < 0 and int_list[i] < 0:
喵2 += 0.3*(int_list[i] - int_list[i+1])
else:
喵2 += 0.3*(int_list[i] - int_list[i+1] - 1)
print(喵1 + 喵2)
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn
小鱼队长测试数据:
'''
测试数据 输出结果
1,11,1 13.0
-1,1 1
1,-1 0.3
-3,12,-3 18.2
-3,-2,-1,1,2,3,4,5,6,7,8 10
大家可以在评论区说出自己有没有答对[doge]
'''点赞0
评论