我必须接受由空格分隔的3个整数输入,例如。3 4 5,并从下一个中减去每个后续的。例如4中的3和5中的4,然后将结果相加。
有人知道我会怎么做吗?有人问我这个问题,如果你感兴趣的话:
林兰是一个巨大的无边无际的国家,位于牛轴上。林兰有三个城市。城市A有坐标xA,城市B有坐标xB,城市C有坐标xC。
旅行者克洛伊住在城市A,她想严格按照这个顺序先去城市B,然后再去城市C。但为了准备这次旅行,她现在需要提前旅行的距离。
给定坐标xA, xB, xC,找到克洛伊从A市到B市,然后从B市到C市所需的距离。
输入第一行包含三个空格分隔的整数: xA, xB, xC (1 ≤ xA, xB, xC ≤ 100) -城市A,B和C的坐标。
输出打印一个整数-克洛伊从城市A到城市B,然后从城市B到城市C的距离。
发布于 2014-04-02 04:34:38
您的输入将采用字符串形式,您可以使用str.split(sep)拆分字符串
def distance(start,end):
# I'll leave implementation of this to you
# use the distance formula if you want to impress your teacher
# but since Lineland lies entirely upon one axis, this shouldn't
# be very hard for you :)
# if in_ is your input
xA, xB, xC = in_.split(" ")
# you could also do = map(int,in_.split(" ")) to avoid the int() calls below
# but frankly I think using the map function is a lesson better suited for
# later.
chloes_travel_time = distance(int(xA),int(xB)) + distance(int(xB),int(xC))
print(chloes_travel_time)https://stackoverflow.com/questions/22796274
复制相似问题