import time import sys A=[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51] Long=len(A) u=37 a=0 b=Long m=1 print('二分查找') time.sleep(1) print('要查找的数列是:',A) time.sleep(1) print('要查找的数是:'+str(u)) time.sleep(1) while a c=b+a c=c/2 c=int(c) if A[c] a=c+1 print(f'第{m}轮查找,查找数列第{c}项:'+str(A[c])) m=m+1 time.sleep(1) elif A[c]==u: print(f'第{m}轮查找,查找数列第{c}项:'+str(A[c])) time.sleep(1) print('能找到') time.sleep(1) sys.exit() else: b=c-1 print(f'第{m}轮查找,查找数列第{c}项:'+str(A[c])) time.sleep(1) m=m+1 time.sleep(1) print('无法找到')
以上就是Python3里的二分查找,你们可以试一下(注:A是要查找的数列,u是要查找的数)
本站作者已申明原创,禁止转载!
文章内容属作者个人观点,不代表本站立场,如有侵权立删。