{"rsdb":{"rid":"397507","subhead":"","postdate":"0","aid":"273598","fid":"77","uid":"1","topic":"1","content":"
\n

\u4ee5\u4e0b\u662f\u4e00\u4e2aPython\u5b9e\u73b0\u7684\u7b80\u5355\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p> \n

def binary_search(arr, target):\n    left, right = 0, len(arr) - 1\n    \n    while left <= right:\n        mid = (left + right) \/\/ 2  # \u627e\u5230\u4e2d\u95f4\u5143\u7d20\u7684\u7d22\u5f15\n        \n        if arr[mid] == target:  # \u5982\u679c\u4e2d\u95f4\u5143\u7d20\u7b49\u4e8e\u76ee\u6807\u503c\uff0c\u8fd4\u56de\u4e2d\u95f4\u5143\u7d20\u7684\u7d22\u5f15\n            return mid\n        elif arr[mid] < target:  # \u5982\u679c\u4e2d\u95f4\u5143\u7d20\u5c0f\u4e8e\u76ee\u6807\u503c\uff0c\u5c06\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u5230\u53f3\u534a\u90e8\u5206\n            left = mid + 1\n        else:  # \u5982\u679c\u4e2d\u95f4\u5143\u7d20\u5927\u4e8e\u76ee\u6807\u503c\uff0c\u5c06\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u5230\u5de6\u534a\u90e8\u5206\n            right = mid - 1\n    \n    return -1  # \u5982\u679c\u672a\u627e\u5230\u76ee\u6807\u503c\uff0c\u8fd4\u56de-1\n\n# \u6d4b\u8bd5\nmy_list = [1, 3, 5, 7, 9, 11, 13, 15]\ntarget_value = 7\n\nresult = binary_search(my_list, target_value)\n\nif result != -1:\n    print(f"\u76ee\u6807\u503c {target_value} \u5728\u5217\u8868\u4e2d\u7684\u7d22\u5f15\u662f {result}")\nelse:\n    print(f"\u76ee\u6807\u503c {target_value} \u672a\u5728\u5217\u8868\u4e2d\u627e\u5230")\n<\/code><\/pre> \n 

\u8fd9\u4e2a\u4ee3\u7801\u793a\u4f8b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff0c\u7528\u4e8e\u5728\u5df2\u6392\u5e8f\u7684\u5217\u8868\u4e2d\u67e5\u627e\u76ee\u6807\u503c\u3002\u5b83\u901a\u8fc7\u4e0d\u65ad\u5c06\u641c\u7d22\u8303\u56f4\u7f29\u5c0f\u4e3a\u5de6\u534a\u90e8\u5206\u6216\u53f3\u534a\u90e8\u5206\u6765\u5feb\u901f\u5b9a\u4f4d\u76ee\u6807\u503c\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u67e5\u627e\u6548\u7387\u3002\u5982\u679c\u627e\u5230\u76ee\u6807\u503c\uff0c\u5b83\u4f1a\u8fd4\u56de\u76ee\u6807\u503c\u7684\u7d22\u5f15\uff0c\u5426\u5219\u8fd4\u56de-1\u3002<\/p> \n<\/div>","orderid":"0","title":"chatgpt\u4f7f\u7528python\u5199\u4e00\u6bb5\u4e8c\u5206\u67e5\u627e\u7684demo","smalltitle":"","mid":"0","fname":"Python","special_id":"0","bak_id":"0","info":"0","hits":"511","pages":"1","comments":"0","posttime":"2023-09-23 15:44:25","list":"1695455065","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"chatgpt<\/A> \u4f7f\u7528<\/A> python<\/A> \u6bb5\u4e8c\u5206<\/A> \u67e5\u627e\u7684<\/A> demo<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"112.94.1.100","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"chatgpt\u4f7f\u7528python\u5199\u4e00\u6bb5\u4e8c\u5206\u67e5\u627e\u7684demo","lastview":"1713291131","digg_num":"537","digg_time":"1712903248","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}