標籤:python zoj c++
我就直接貼代碼了,代碼上有具體的思路。
# -*- 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()
python實現ZOJ1745(簡單類比)