python实现ZOJ1745(简单模拟)

来源:互联网 发布:北京语言大学网络继续 编辑:程序博客网 时间:2024/06/06 02:19

我就直接贴代码了,代码上有具体的思路。

# -*- coding:utf-8 -*-'''每一行输入最少两个数最多21个数,且最后一步一定要到达饼干。每一行输入的第一个数是饼干所在的位置,且饼干的位置不能为0.输出有三种状态,输出什么状态,取决于这一次和上一次距离饼干的距离是否近了还是远了还是相同近了返回warmer远了返回colder如果相同则返回same如果输入的数字与饼干所在位置相同则输出found it!最后如果输入5280则代表程序结束'''import sysdef 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()
0 0