{"id":257244,"date":"2024-05-15T12:07:11","date_gmt":"2024-05-15T04:07:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/257244.html"},"modified":"2024-05-15T12:07:16","modified_gmt":"2024-05-15T04:07:16","slug":"%e5%a6%82%e4%bd%95%e7%94%a8%e4%bb%a3%e7%a0%81%e5%87%bd%e6%95%b0%e5%ae%9e%e7%8e%b0%e4%ba%8c%e8%bf%9b%e5%88%b6%e8%bd%ac%e5%8d%81%e8%bf%9b%e5%88%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/257244.html","title":{"rendered":"\u5982\u4f55\u7528\u4ee3\u7801\u51fd\u6570\u5b9e\u73b0\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27103517\/5c89a57a-ba50-4583-804a-8e5362fd70d5.webp\" alt=\"\u5982\u4f55\u7528\u4ee3\u7801\u51fd\u6570\u5b9e\u73b0\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\" \/><\/p>\n<p><p>\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u8f6c\u6362\u6d89\u53ca\u5230\u7684\u6838\u5fc3\u6982\u5ff5\u4e3b\u8981\u662f<strong>\u6309\u6743\u5c55\u5f00\u6cd5<\/strong>\u3001<strong>\u4f4d\u6743\u91cd<\/strong>\u53ca<strong>\u8fed\u4ee3\u7d2f\u52a0<\/strong>\u3002\u5728\u8fd9\u5176\u4e2d\uff0c<strong>\u6309\u6743\u5c55\u5f00\u6cd5<\/strong>\u662f\u6b64\u8fc7\u7a0b\u7684\u5173\u952e\u3002\u5b83\u6307\u7684\u662f\u5c06\u4e8c\u8fdb\u5236\u6bcf\u4f4d\u6570\u4e58\u4ee52\u7684\uff08\u4f4d\u6570-1\uff09\u6b21\u5e42\uff0c\u7136\u540e\u5c06\u7ed3\u679c\u7d2f\u52a0\uff0c\u6700\u7ec8\u5f97\u5230\u7684\u548c\u5c31\u662f\u5bf9\u5e94\u7684\u5341\u8fdb\u5236\u6570\u503c\u3002\u8fd9\u79cd\u65b9\u6cd5\u65e2\u9002\u7528\u4e8e\u6574\u6570\u90e8\u5206\uff0c\u4e5f\u9002\u7528\u4e8e\u5c0f\u6570\u90e8\u5206\u7684\u8f6c\u6362\uff0c\u53ea\u4e0d\u8fc7\u5c0f\u6570\u90e8\u5206\u7684\u6743\u91cd\u662f\u8d1f\u7684\u3002\u5728\u7f16\u7a0b\u5b9e\u73b0\u4e0a\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u91c7\u7528\u5faa\u73af\u6216\u9012\u5f52\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6bcf\u4e00\u4f4d\uff0c\u9010\u6b65\u7d2f\u52a0\u5f97\u5230\u6700\u7ec8\u7684\u5341\u8fdb\u5236\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3\u4e8c\u8fdb\u5236\u4e0e\u5341\u8fdb\u5236<\/h3>\n<\/p>\n<p><p>\u4e8c\u8fdb\u5236\u662f\u8ba1\u7b97\u673a\u4e2d\u4f7f\u7528\u7684\u6570\u5236\uff0c\u4e3b\u8981\u75310\u548c1\u4e24\u79cd\u6570\u5b57\u7b26\u53f7\u6784\u6210\u3002\u6bcf\u4e00\u4f4d\u7684\u5347\u9ad8\u90fd\u4ee3\u8868\u7740\u6743\u503c\u500d\u589e\uff0c\u7c7b\u4f3c\u5341\u8fdb\u5236\u4e2d\u7684\u6bcf\u4e00\u4f4d\u4ee3\u8868\u774010\u7684\u5e42\u6b21\u65b9\u589e\u52a0\u3002\u5728\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6700\u6839\u672c\u7684\u662f\u7406\u89e3\u6bcf\u4e00\u4f4d\u7684\u4e8c\u8fdb\u5236\u6570\u4ee3\u8868\u7684\u5177\u4f53\u5341\u8fdb\u5236\u6743\u503c\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u7406\u89e3\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u8f6c\u6362\u524d\uff0c\u6211\u4eec\u9700\u8981\u660e\u786e\u6bcf\u4e00\u4f4d\u4e0a\u7684\u4e8c\u8fdb\u5236\u6570\uff080\u62161\uff09\u6240\u4ee3\u8868\u7684\u5b9e\u9645\u5341\u8fdb\u5236\u503c\u3002\u8fd9\u4e9b\u503c\u662f\u901a\u8fc72\u7684n\u6b21\u65b9\u8ba1\u7b97\u5f97\u51fa\u7684\uff0c\u5176\u4e2dn\u4e3a\u4e8c\u8fdb\u5236\u6570\u6240\u5728\u7684\u4f4d\u7f6e\u7d22\u5f15\uff08\u4ece0\u5f00\u59cb\u8ba1\u6570\uff09\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u80fd\u591f\u638c\u63e1\u6bcf\u4e00\u4f4d\u4e8c\u8fdb\u5236\u6570\u7684\u201c\u6743\u91cd\u201d\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6309\u6743\u5c55\u5f00\u6cd5\u7684\u7f16\u7a0b\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p><strong>\u6309\u6743\u5c55\u5f00\u6cd5<\/strong>\u662f\u5b9e\u73b0\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u8f6c\u6362\u7684\u5173\u952e\u6280\u672f\u3002\u6b64\u65b9\u6cd5\u4e3b\u8981\u901a\u8fc7\u904d\u5386\u4e8c\u8fdb\u5236\u6570\u7684\u6bcf\u4e00\u4f4d\uff0c\u5c06\u5176\u8f6c\u6362\u6210\u5bf9\u5e94\u7684\u5341\u8fdb\u5236\u6570\uff0c\u5e76\u6839\u636e\u4e8c\u8fdb\u5236\u4f4d\u7684\u4f4d\u7f6e\u8ba1\u7b97\u51fa\u6bcf\u4e00\u4f4d\u7684\u6743\u503c\uff0c\u7136\u540e\u8fdb\u884c\u7d2f\u52a0\u3002<\/p>\n<\/p>\n<p><p>\u7f16\u7a0b\u5b9e\u73b0\u65f6\uff0c\u9996\u5148\u9700\u8981\u5c06\u4e8c\u8fdb\u5236\u6570\u4f5c\u4e3a\u5b57\u7b26\u4e32\u6216\u6570\u7ec4\u5904\u7406\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u6bcf\u4e00\u4f4d\u8fdb\u884c\u64cd\u4f5c\u3002\u904d\u5386\u8fd9\u4e2a\u5b57\u7b26\u4e32\u6216\u6570\u7ec4\uff0c\u4ece\u6700\u53f3\u4fa7\u7684\u4e00\u4f4d\u5f00\u59cb\uff0c\u5373\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\u5904\u7406\u3002\u5bf9\u4e8e\u4f4d\u4e8e\u7d22\u5f15<code>i<\/code>\u7684\u6570\u503c\uff0c\u5176\u6743\u91cd\u4e3a<code>2^i<\/code>\u3002\u901a\u8fc7\u7d2f\u52a0\u8fd9\u4e9b\u6743\u91cd\u503c\uff0c\u5373\u53ef\u5f97\u5230\u6700\u7ec8\u7684\u5341\u8fdb\u5236\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u73b0\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">def binary_to_decimal(binary_str):<\/p>\n<p>    decimal = 0<\/p>\n<p>    for i, digit in enumerate(binary_str[::-1]):<\/p>\n<p>        if digit == &#039;1&#039;:<\/p>\n<p>            decimal += 2i<\/p>\n<p>    return decimal<\/p>\n<h2><strong>\u4f7f\u7528\u793a\u4f8b<\/strong><\/h2>\n<p>binary_str = &quot;1101&quot;<\/p>\n<p>print(f&quot;\u4e8c\u8fdb\u5236\u6570 {binary_str} \u5bf9\u5e94\u7684\u5341\u8fdb\u5236\u6570\u662f {binary_to_decimal(binary_str)}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c<code>binary_to_decimal<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u4ee3\u8868\u4e8c\u8fdb\u5236\u6570\u7684\u5b57\u7b26\u4e32<code>binary_str<\/code>\u3002\u901a\u8fc7\u679a\u4e3e\u8fd9\u4e2a\u5b57\u7b26\u4e32\u7684\u53cd\u5411\u526f\u672c\uff08\u4ee5\u4fbf\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\u5904\u7406\uff09\uff0c\u51fd\u6570\u8ba1\u7b97\u6bcf\u4f4d\u7684\u5341\u8fdb\u5236\u6743\u503c\uff0c\u5e76\u7d2f\u52a0\u5230<code>decimal<\/code>\u53d8\u91cf\u4e2d\u3002\u8fd9\u4e2a\u7b80\u5355\u7684\u5faa\u73af\u7ed3\u6784\u5145\u5206\u4f53\u73b0\u4e86\u6309\u6743\u5c55\u5f00\u6cd5\u7684\u5b9e\u8d28\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fb9\u754c\u60c5\u51b5\u4e0e\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u7f16\u5199\u7528\u4e8e\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u4ee3\u7801\u65f6\uff0c\u8003\u8651\u8fb9\u754c\u60c5\u51b5\u548c\u6027\u80fd\u4f18\u5316\u540c\u6837\u91cd\u8981\u3002\u4f8b\u5982\uff0c\u8f93\u5165\u7684\u4e8c\u8fdb\u5236\u6570\u53ef\u80fd\u5305\u542b\u975e\u6cd5\u5b57\u7b26\uff08\u975e0\u62161\uff09\uff0c\u8fd9\u5c31\u8981\u6c42\u5728\u8f6c\u6362\u524d\u8fdb\u884c\u6709\u6548\u6027\u6821\u9a8c\u3002<\/p>\n<\/p>\n<p><p>\u53e6\u5916\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u4e8c\u8fdb\u5236\u6570\u8f6c\u6362\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u4f4d\u8fd0\u7b97\u7b26\u6765\u4f18\u5316\u6027\u80fd\u3002\u4f4d\u8fd0\u7b97\u7b26\u76f4\u63a5\u5728\u6570\u7684\u4e8c\u8fdb\u5236\u5f62\u5f0f\u4e0a\u64cd\u4f5c\uff0c\u56e0\u800c\u80fd\u591f\u63d0\u4f9b\u66f4\u9ad8\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def binary_to_decimal_optimized(binary_str):<\/p>\n<p>    decimal = 0<\/p>\n<p>    for digit in binary_str:<\/p>\n<p>        decimal = decimal * 2 + int(digit)<\/p>\n<p>    return decimal<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f18\u5316\u7248\u672c\u5229\u7528\u4e86\u4f4d\u79fb\u548c\u52a0\u6cd5\u64cd\u4f5c\u6765\u907f\u514d\u663e\u5f0f\u7684\u5e42\u8fd0\u7b97\uff0c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u4e86\u8f6c\u6362\u6548\u7387\u3002\u901a\u8fc7\u5c06\u7d2f\u8ba1\u503c\u4e0d\u65ad\u5730\u4e58\u4ee52\uff08\u7b49\u540c\u4e8e\u5de6\u79fb\u4e00\u4f4d\uff09\uff0c\u518d\u52a0\u4e0a\u5f53\u524d\u4f4d\u7684\u503c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u6709\u6548\u4f46\u6027\u80fd\u66f4\u4f73\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u4e0e\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7f16\u7a0b\u5b9e\u73b0\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u8f6c\u6362\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u6280\u672f\u4e0a\u7684\u7ec3\u4e60\uff0c\u5b83\u4e5f\u5e2e\u52a9\u6211\u4eec\u66f4\u6df1\u5165\u5730\u7406\u89e3\u6570\u5b57\u7cfb\u7edf\u7684\u5de5\u4f5c\u539f\u7406\u3002\u65e0\u8bba\u662f\u5728\u6570\u636e\u5904\u7406\u3001\u901a\u4fe1\u7cfb\u7edf\u8bbe\u8ba1\uff0c\u8fd8\u662f\u5728\u7f16\u5199\u5e95\u5c42\u7cfb\u7edf\u8f6f\u4ef6\u65f6\uff0c\u8fd9\u79cd\u57fa\u672c\u7684\u6570\u5236\u8f6c\u6362\u903b\u8f91\u90fd\u4f1a\u7ecf\u5e38\u7528\u5230\u3002\u638c\u63e1\u548c\u7406\u89e3\u5b83\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u8f6f\u4ef6\u5f00\u53d1\u8005\u90fd\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u8fdb\u5236\u548c\u5341\u8fdb\u5236\u4e4b\u95f4\u7684\u8f6c\u6362\u65f6\uff0c\u6211\u4eec\u4e0d\u5e94\u53ea\u6ee1\u8db3\u4e8e\u4f7f\u7528\u73b0\u6210\u7684\u51fd\u6570\u6216\u65b9\u6cd5\uff0c\u800c\u5e94\u8be5\u6df1\u5165\u7406\u89e3\u5176\u80cc\u540e\u7684\u6570\u5b66\u539f\u7406\u548c\u903b\u8f91\u3002\u8fd9\u79cd\u7406\u89e3\u80fd\u591f\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u9762\u5bf9\u66f4\u4e3a\u590d\u6742\u7684\u6570\u5b57\u5904\u7406\u95ee\u9898\u65f6\uff0c\u80fd\u591f\u66f4\u52a0\u7075\u6d3b\u548c\u6709\u6548\u5730\u8fdb\u884c\u89e3\u51b3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u4ee3\u7801\u51fd\u6570\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">def binary_to_decimal(binary):\n    decimal = 0\n    power = 0\n\n    while binary &gt; 0:\n        decimal += (binary % 10) * (2 ** power)\n        binary \/\/= 10\n        power += 1\n\n    return decimal\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684\u5341\u8fdb\u5236\u6570\u3002\u5b83\u4f7f\u7528\u4e86\u4e00\u4e2a\u5faa\u73af\u548c\u53d6\u4f59\u8fd0\u7b97\u6765\u9010\u4f4d\u8ba1\u7b97\uff0c\u6bcf\u4e00\u4f4d\u4e0a\u7684\u4e8c\u8fdb\u5236\u6570\u4e58\u4ee5\u5bf9\u5e94\u7684\u6743\u91cd\u540e\u7d2f\u52a0\u8d77\u6765\uff0c\u6700\u540e\u5f97\u5230\u5341\u8fdb\u5236\u7ed3\u679c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5c06\u8f93\u5165\u7684\u4e8c\u8fdb\u5236\u6570\u4f20\u9012\u7ed9\u8f6c\u6362\u51fd\u6570\uff1f<\/strong><br \/>\n\u60a8\u53ef\u4ee5\u5728\u8c03\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u5c06\u9700\u8981\u8f6c\u6362\u7684\u4e8c\u8fdb\u5236\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u60a8\u60f3\u5c06\u4e8c\u8fdb\u5236\u65701101\u8f6c\u6362\u4e3a\u5341\u8fdb\u5236\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u8c03\u7528\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-python\">binary_number = 1101\ndecimal_number = binary_to_decimal(binary_number)\nprint(decimal_number)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u5341\u8fdb\u5236\u657013\u3002<\/p>\n<p><strong>3. \u8be5\u51fd\u6570\u9002\u7528\u4e8e\u4efb\u4f55\u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u5417\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u4e0a\u8ff0\u7684\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u51fd\u6570\u9002\u7528\u4e8e\u4efb\u4f55\u957f\u5ea6\u7684\u4e8c\u8fdb\u5236\u6570\u3002\u5b83\u4f7f\u7528\u5faa\u73af\u548c\u53d6\u4f59\u8fd0\u7b97\u6765\u9010\u4f4d\u8ba1\u7b97\uff0c\u5e76\u4e14\u4e0d\u4f9d\u8d56\u4e8e\u4e8c\u8fdb\u5236\u6570\u7684\u957f\u5ea6\u3002\u65e0\u8bba\u8f93\u5165\u7684\u4e8c\u8fdb\u5236\u6570\u6709\u591a\u5c11\u4f4d\uff0c\u51fd\u6570\u90fd\u80fd\u6b63\u786e\u5730\u5c06\u5176\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5341\u8fdb\u5236\u6570\u3002\u56e0\u6b64\uff0c\u60a8\u53ef\u4ee5\u653e\u5fc3\u4f7f\u7528\u8be5\u51fd\u6570\u8fdb\u884c\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u8ba1\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e8c\u8fdb\u5236\u8f6c\u5341\u8fdb\u5236\u7684\u8f6c\u6362\u6d89\u53ca\u5230\u7684\u6838\u5fc3\u6982\u5ff5\u4e3b\u8981\u662f\u6309\u6743\u5c55\u5f00\u6cd5\u3001\u4f4d\u6743\u91cd\u53ca\u8fed\u4ee3\u7d2f\u52a0\u3002\u5728\u8fd9\u5176\u4e2d\uff0c\u6309\u6743\u5c55\u5f00\u6cd5\u662f\u6b64\u8fc7\u7a0b\u7684\u5173\u952e\u3002\u5b83 [&hellip;]","protected":false},"author":3,"featured_media":257253,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/257244"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=257244"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/257244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/257253"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=257244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=257244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=257244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}