二分法求解问题

import sys

final_string = ''
final_count = 0

class BinarySearch:
    def getPos(self, val):
        A = []
        for i in xrange(0, 181):
            A.append(i-90)        
        self.findPos(A, 0, 180, val)
        print final_string
    
    def findPos(self, A, start, end, value):
        final_count = final_count + 1
        if final_count > 6:
            return

        if start == end:
            if (A[end] == value):
                final_string = final_string+'1'
                return
            else:
                return
            
        if A[(end+start)/2] == value:
            final_string = final_string+'1'
            return
        elif A[(end+start)/2] < value:
            final_string = final_string+'0'
            return self.findPos(A, (end+start)/2+1, end, value)
        else:
            final_string = final_string+'1'
            return self.findPos(A, start, (end+start)/2-1, value)

if __name__ == '__main__':
    for line in sys.stdin:
        BinarySearch b
        b.getPos(int(line))

'''
二分法求解
'''

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部