{"id":1094035,"date":"2025-01-08T14:37:04","date_gmt":"2025-01-08T06:37:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094035.html"},"modified":"2025-01-08T14:37:07","modified_gmt":"2025-01-08T06:37:07","slug":"%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e6%b0%b4%e4%bb%99%e8%8a%b1%e6%95%b0python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094035.html","title":{"rendered":"\u5982\u4f55\u8f93\u51fa\u6c34\u4ed9\u82b1\u6570python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210342\/1ffc4e0d-e4a3-4075-acd5-5b57ec3cfc86.webp\" alt=\"\u5982\u4f55\u8f93\u51fa\u6c34\u4ed9\u82b1\u6570python\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u8f93\u51fa\u6c34\u4ed9\u82b1\u6570python\uff1a\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u3001\u5224\u65ad\u6bcf\u4e2a\u6570\u5b57\u7684\u7acb\u65b9\u548c\u662f\u5426\u7b49\u4e8e\u8be5\u6570\u3001\u8f93\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u3002\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u53ef\u4ee5\u904d\u5386\u6240\u6709\u4e09\u4f4d\u6570\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u4e2a\u6570\u7684\u6bcf\u4e2a\u4f4d\u4e0a\u7684\u7acb\u65b9\u548c\uff0c\u5982\u679c\u548c\u7b49\u4e8e\u8be5\u6570\uff0c\u5219\u8f93\u51fa\u8fd9\u4e2a\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63cf\u8ff0\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u7b80\u8981\u56de\u7b54\u4e00\u4e0b\u6807\u9898\u95ee\u9898\u3002<strong>\u8981\u8f93\u51fa\u6c34\u4ed9\u82b1\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u7a0b\uff0c\u901a\u8fc7\u4e09\u91cd\u5faa\u73af\u904d\u5386\u6240\u6709\u4e09\u4f4d\u6570\uff0c\u8ba1\u7b97\u5b83\u4eec\u5404\u4e2a\u4f4d\u6570\u7684\u7acb\u65b9\u548c\uff0c\u5224\u65ad\u662f\u5426\u7b49\u4e8e\u539f\u6570\uff0c\u5982\u679c\u662f\u5219\u8f93\u51fa\u8be5\u6570<\/strong>\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u901a\u8fc7Python\u4ee3\u7801\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4ec0\u4e48\u662f\u6c34\u4ed9\u82b1\u6570<\/h3>\n<\/p>\n<p><p>\u6c34\u4ed9\u82b1\u6570\uff08Narcissistic number\uff09\u53c8\u79f0\u4e3a\u81ea\u604b\u6570\u3001\u81ea\u5e42\u6570\u3001\u81ea\u604b\u6570\u7b49\uff0c\u662f\u6307\u4e00n\u4f4d\u6570\uff0c\u5b83\u7684\u6bcf\u4e2a\u4f4d\u4e0a\u7684\u6570\u5b57\u7684n\u6b21\u5e42\u4e4b\u548c\u7b49\u4e8e\u5b83\u672c\u8eab\u3002\u5bf9\u4e8e\u4e09\u4f4d\u6570\u6765\u8bf4\uff0c\u6c34\u4ed9\u82b1\u6570\u662f\u6307\u4e00\u4e2a\u4e09\u4f4d\u6570\uff0c\u5176\u5404\u4e2a\u6570\u4f4d\u7684\u7acb\u65b9\u548c\u7b49\u4e8e\u8be5\u6570\u672c\u8eab\u3002\u4f8b\u5982\uff0c153 \u662f\u4e00\u4e2a\u6c34\u4ed9\u82b1\u6570\uff0c\u56e0\u4e3a 1^3 + 5^3 + 3^3 = 153\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6c34\u4ed9\u82b1\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u5b9e\u73b0\u6c34\u4ed9\u82b1\u6570\u7684\u8f93\u51fa\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u4e09\u91cd\u5faa\u73af\u904d\u5386\u6240\u6709\u4e09\u4f4d\u6570\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u4e2a\u6570\u7684\u6bcf\u4e2a\u4f4d\u4e0a\u7684\u7acb\u65b9\u548c\uff0c\u5224\u65ad\u662f\u5426\u7b49\u4e8e\u539f\u6570\u3002\u5982\u679c\u662f\uff0c\u5219\u8f93\u51fa\u8be5\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8f93\u51fa\u6240\u6709\u7684\u6c34\u4ed9\u82b1\u6570<\/p>\n<p>for i in range(100, 1000):<\/p>\n<p>    # \u5206\u89e3\u4e2a\u4f4d\u3001\u5341\u4f4d\u3001\u767e\u4f4d<\/p>\n<p>    hundreds = i \/\/ 100<\/p>\n<p>    tens = (i % 100) \/\/ 10<\/p>\n<p>    units = i % 10<\/p>\n<p>    # \u8ba1\u7b97\u5404\u4e2a\u4f4d\u6570\u7acb\u65b9\u548c<\/p>\n<p>    if i == hundreds &lt;strong&gt; 3 + tens &lt;\/strong&gt; 3 + units  3:<\/p>\n<p>        print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8be6\u7ec6\u89e3\u6790\u4ee3\u7801\u5b9e\u73b0\u6b65\u9aa4<\/h3>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u6240\u6709\u4e09\u4f4d\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u6240\u6709\u4e09\u4f4d\u6570\uff0c\u4ece100\u5230999\u3002\u8303\u56f4\u51fd\u6570<code>range(100, 1000)<\/code>\u751f\u6210\u4e00\u4e2a\u4ece100\u5230999\u7684\u6574\u6570\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5206\u89e3\u6bcf\u4e2a\u6570\u5b57\u4e3a\u4e2a\u4f4d\u3001\u5341\u4f4d\u548c\u767e\u4f4d<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6574\u9664\u548c\u53d6\u4f59\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u6bcf\u4e2a\u6570\u5206\u89e3\u4e3a\u4e2a\u4f4d\u3001\u5341\u4f4d\u548c\u767e\u4f4d\u3002\u4f8b\u5982\uff0c123 \u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5206\u89e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">hundreds = 123 \/\/ 100  # \u767e\u4f4d<\/p>\n<p>tens = (123 % 100) \/\/ 10  # \u5341\u4f4d<\/p>\n<p>units = 123 % 10  # \u4e2a\u4f4d<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u8ba1\u7b97\u5404\u4e2a\u4f4d\u6570\u7acb\u65b9\u548c<\/h4>\n<\/p>\n<p><p>\u8ba1\u7b97\u5404\u4e2a\u4f4d\u6570\u7684\u7acb\u65b9\u548c\uff0c\u7136\u540e\u5224\u65ad\u8fd9\u4e2a\u548c\u662f\u5426\u7b49\u4e8e\u539f\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if i == hundreds &lt;strong&gt; 3 + tens &lt;\/strong&gt; 3 + units  3:<\/p>\n<p>    print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u8f93\u51fa\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u7b26\u5408\u6761\u4ef6\u7684\u6570\u5373\u4e3a\u6c34\u4ed9\u82b1\u6570\uff0c\u6211\u4eec\u5c06\u5176\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6269\u5c55\u5230\u4efb\u610f\u4f4d\u6570\u7684\u6c34\u4ed9\u82b1\u6570<\/h3>\n<\/p>\n<p><p>\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u4e09\u4f4d\u6570\u7684\u6c34\u4ed9\u82b1\u6570\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u627e\u51fa\u4efb\u610f\u4f4d\u6570\u7684\u6c34\u4ed9\u82b1\u6570\uff0c\u53ef\u4ee5\u5c06\u4ee3\u7801\u8fdb\u884c\u6269\u5c55\uff0c\u5e76\u4f7f\u7528\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u627e\u51fa\u6240\u6709\u56db\u4f4d\u6570\u6c34\u4ed9\u82b1\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_narcissistic(number):<\/p>\n<p>    # \u5c06\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u65b9\u4fbf\u9010\u4f4d\u64cd\u4f5c<\/p>\n<p>    digits = list(map(int, str(number)))<\/p>\n<p>    length = len(digits)<\/p>\n<p>    # \u8ba1\u7b97\u5404\u4e2a\u4f4d\u6570\u7684length\u6b21\u5e42\u7684\u548c<\/p>\n<p>    return number == sum([digit  length for digit in digits])<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u7684\u56db\u4f4d\u6570\u6c34\u4ed9\u82b1\u6570<\/strong><\/h2>\n<p>for i in range(1000, 10000):<\/p>\n<p>    if is_narcissistic(i):<\/p>\n<p>        print(i)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u6bb5\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>is_narcissistic<\/code>\uff0c\u53ef\u4ee5\u5224\u65ad\u4efb\u610f\u6570\u5b57\u662f\u5426\u4e3a\u6c34\u4ed9\u82b1\u6570\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u904d\u5386\u6240\u6709\u56db\u4f4d\u6570\u5e76\u8f93\u51fa\u6240\u6709\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u4ee3\u7801\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u4f18\u5316\u624b\u6bb5\u6765\u63d0\u9ad8\u4ee3\u7801\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u751f\u6210\u5f0f\u6765\u7b80\u5316\u4ee3\u7801\uff0c\u5e76\u51cf\u5c11\u8ba1\u7b97\u6b21\u6570\u3002\u4ee5\u4e0b\u662f\u4f18\u5316\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def find_narcissistic_numbers(start, end):<\/p>\n<p>    # \u5b58\u50a8\u7ed3\u679c<\/p>\n<p>    result = []<\/p>\n<p>    # \u904d\u5386\u8303\u56f4\u5185\u7684\u6240\u6709\u6570\u5b57<\/p>\n<p>    for number in range(start, end + 1):<\/p>\n<p>        digits = list(map(int, str(number)))<\/p>\n<p>        length = len(digits)<\/p>\n<p>        if number == sum(digit  length for digit in digits):<\/p>\n<p>            result.append(number)<\/p>\n<p>    return result<\/p>\n<h2><strong>\u8f93\u51fa\u6240\u6709\u4e09\u4f4d\u6570\u548c\u56db\u4f4d\u6570\u7684\u6c34\u4ed9\u82b1\u6570<\/strong><\/h2>\n<p>narcissistic_numbers = find_narcissistic_numbers(100, 9999)<\/p>\n<p>print(narcissistic_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f18\u5316\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u5217\u8868\u751f\u6210\u5f0f\u548c\u51fd\u6570\u5c01\u88c5\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u6c34\u4ed9\u82b1\u6570\u7684\u8f93\u51fa\u3002<strong>\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u3001\u8ba1\u7b97\u5404\u4e2a\u4f4d\u6570\u7684\u7acb\u65b9\u548c\u3001\u5224\u65ad\u662f\u5426\u7b49\u4e8e\u539f\u6570<\/strong>\uff0c\u8fd9\u662f\u5b9e\u73b0\u6c34\u4ed9\u82b1\u6570\u8f93\u51fa\u7684\u6838\u5fc3\u6b65\u9aa4\u3002\u6211\u4eec\u8fd8\u5c55\u793a\u4e86\u5982\u4f55\u6269\u5c55\u5230\u4efb\u610f\u4f4d\u6570\u7684\u6c34\u4ed9\u82b1\u6570\uff0c\u5e76\u8fdb\u884c\u4e86\u4ee3\u7801\u4f18\u5316\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7406\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u5e2e\u52a9\u6211\u4eec\u89e3\u51b3\u7c7b\u4f3c\u7684\u6570\u5b66\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662f\u6c34\u4ed9\u82b1\u6570\uff0c\u5982\u4f55\u5b9a\u4e49\u5b83\uff1f<\/strong><br \/>\u6c34\u4ed9\u82b1\u6570\u662f\u6307\u4e00\u4e2an\u4f4d\u6570\uff0c\u5176\u5404\u4f4d\u6570\u5b57\u7684n\u6b21\u65b9\u4e4b\u548c\u7b49\u4e8e\u5b83\u672c\u8eab\u3002\u4f8b\u5982\uff0c153\u662f\u4e00\u4e2a\u6c34\u4ed9\u82b1\u6570\uff0c\u56e0\u4e3a1^3 + 5^3 + 3^3 = 153\u3002\u4e86\u89e3\u6c34\u4ed9\u82b1\u6570\u7684\u5b9a\u4e49\u6709\u52a9\u4e8e\u6211\u4eec\u66f4\u597d\u5730\u7f16\u5199\u76f8\u5173\u7684Python\u4ee3\u7801\u6765\u67e5\u627e\u8fd9\u4e9b\u7279\u6b8a\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5b9e\u73b0\u67e5\u627e\u6c34\u4ed9\u82b1\u6570\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\u6765\u904d\u5386\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c\u8ba1\u7b97\u5404\u4e2a\u6570\u5b57\u7684\u4f4d\u6570\u548c\u4f4d\u6570\u6b21\u65b9\u548c\uff0c\u5224\u65ad\u662f\u5426\u4e3a\u6c34\u4ed9\u82b1\u6570\u3002\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u662f\uff1a  <\/p>\n<pre><code class=\"language-python\">for num in range(100, 1000):\n    digits = [int(d) for d in str(num)]\n    if sum(d ** len(digits) for d in digits) == num:\n        print(num)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u6253\u5370\u51fa\u6240\u6709\u4e09\u4f4d\u6c34\u4ed9\u82b1\u6570\u3002<\/p>\n<p><strong>\u8f93\u51fa\u6c34\u4ed9\u82b1\u6570\u7684\u6700\u4f73\u5b9e\u8df5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u548c\u5185\u7f6e\u51fd\u6570\u8fdb\u884c\u4f18\u5316\u3002\u6b64\u5916\uff0c\u8bbe\u7f6e\u5408\u7406\u7684\u8303\u56f4\uff0c\u907f\u514d\u904d\u5386\u4e0d\u5fc5\u8981\u7684\u6570\u5b57\uff0c\u53ef\u4ee5\u52a0\u901f\u7a0b\u5e8f\u7684\u8fd0\u884c\u3002\u4f7f\u7528\u51fd\u6570\u5c01\u88c5\u67e5\u627e\u903b\u8f91\u4e5f\u662f\u4e00\u4e2a\u826f\u597d\u7684\u7f16\u7a0b\u4e60\u60ef\uff0c\u6709\u52a9\u4e8e\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u91cd\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u8f93\u51fa\u6c34\u4ed9\u82b1\u6570python\uff1a\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u3001\u5224\u65ad\u6bcf\u4e2a\u6570\u5b57\u7684\u7acb\u65b9\u548c\u662f\u5426\u7b49\u4e8e\u8be5\u6570\u3001\u8f93\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u6570\u3002\u4f7f\u7528\u4e09\u91cd\u5faa\u73af\u53ef [&hellip;]","protected":false},"author":3,"featured_media":1094040,"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\/1094035"}],"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=1094035"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094035\/revisions"}],"predecessor-version":[{"id":1094041,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094035\/revisions\/1094041"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094040"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}