首页 > 编程语言 > 详细

python实现ZOJ1745(简单模拟)

时间:2015-05-13 10:30:41      阅读:514      评论:0      收藏:0      [点我收藏+]

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

# -*- 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()



python实现ZOJ1745(简单模拟)

原文:http://blog.csdn.net/djd1234567/article/details/45678901

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!