我就直接贴代码了,代码上有具体的思路。
# -*- coding:utf-8 -*-
'''
每一行输入最少两个数最多21个数,且最后一步一定要到达饼干。
每一行输入的第一个数是饼干所在的位置,且饼干的位置不能为0.
输出有三种状态,输出什么状态,取决于这一次和上一次距离饼干的距离是否近了还是远了还是相同
近了返回warmer远了返回colder如果相同则返回same
如果输入的数字与饼干所在位置相同则输出found it!
最后如果输入5280则代表程序结束
'''
import sys
def SearchCookie():
while True:
num = sys.stdin.readline().split()
CookiePos = int(num[0])
nowPos = int(num[1])
PastPos = 0
if CookiePos == 5280:
return 0
i = 2
while True:
NowGap = abs(nowPos-CookiePos)
PastGap = abs(PastPos-CookiePos)
if NowGap > PastGap:
print 'Moving from %d to %d: colder.'%(PastPos,nowPos)
elif NowGap < PastGap:
print 'Moving from %d to %d: warmer.'%(PastPos,nowPos)
else:
print 'Moving from %d to %d: same.'%(PastPos,nowPos)
PastPos = nowPos
nowPos = int(num[i])
i = i + 1
if nowPos == CookiePos:
print 'Moving from %d to %d: found it!'%(PastPos,nowPos)
break
if __name__ == '__main__':
SearchCookie()