def check_coin_3(a,right,left): b = (left - right +1) // 3 if right == left: location1 = right + 1 return location1 elif right+1 == left: if a[right] < a[left]: location1 = right + 1 return location1 else: location1 = left + 1 return location1 else: if sum(a[right:right+b]) == sum(a[right+b:right+2*b]): return check_coin_3(a,right+2*b,left) elif sum(a[right:right+b]) > sum(a[right+b:right+2*b]): return check_coin_3(a,right+b,right+2*b-1) else: return check_coin_3(a,right,right+b-1) if __name__ == '__main__': a = [] location1 = 0 i = 0 right0 = 0 left0 = int(input("print left0:")) for i in range(left0): a.append(1) b = int(input("print flause_coin's location :")) a[b-1] = 0 print("The faulse coin is located in ") l1 = check_coin_3(a,right0,left0-1) print(l1)
减治法之三分法处理假币问题-python代码
最新推荐文章于 2023-03-03 09:57:59 发布