{"id":50137,"date":"2024-12-03T17:11:51","date_gmt":"2024-12-03T09:11:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/50137\/"},"modified":"2024-12-03T17:11:51","modified_gmt":"2024-12-03T09:11:51","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8%e6%b1%82%e4%bd%99%e5%92%8c%e6%95%b4%e9%99%a4%e8%ae%a1%e7%ae%97%e6%95%b4%e6%95%b0%e5%90%84%e6%95%b0%e5%ad%97%e4%b9%8b%e5%92%8c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/50137\/","title":{"rendered":"\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241112\/17313914256732efc1ede04.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c<\/strong><\/p>\n<p>\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u91c7\u7528\u4ee5\u4e0b\u7b97\u6cd5\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u6c42\u4f59\u8fd0\u7b97\u7b26\uff08%\uff09\u63d0\u53d6\u6574\u6570\u7684\u672b\u5c3e\u6570\u5b57\u3002<\/li>\n<li>\u4f7f\u7528\u53cc\u659c\u6760\u8fd0\u7b97\u7b26\uff08\/\/\uff09\u53bb\u9664\u672b\u5c3e\u6570\u5b57\uff0c\u66f4\u65b0\u6574\u6570\u3002<\/li>\n<li>\u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u76f4\u5230\u6574\u6570\u53d8\u4e3a\u96f6\u3002<\/li>\n<\/ul>\n<p>\u5b9e\u73b0\u8fd9\u6bb5\u7b97\u6cd5\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>def sum_digits(num):\n  sum = 0\n  while num &gt; 0:\n    sum += num % 10\n    num \/\/= 10\n  return sum<\/pre>\n<p>\u5728\u51fd\u6570 sum_digits \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u91cd\u590d\u6267\u884c\u7b97\u6cd5\u6b65\u9aa4\uff0c\u76f4\u5230 num \u53d8\u6210\u96f6\u3002\u5faa\u73af\u7684\u6761\u4ef6\u662f num &gt; 0\uff0c\u8fd9\u786e\u4fdd\u4e86\u6211\u4eec\u53ea\u4f1a\u63d0\u53d6\u6574\u6570\u4e2d\u7684\u6570\u5b57\uff0c\u76f4\u5230\u5b83\u53d8\u4e3a\u7a7a\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u6765\u6d4b\u8bd5\u51fd\u6570\uff1a<\/p>\n<pre>print(sum_digits(123))  # \u8f93\u51fa\uff1a6\nprint(sum_digits(456))  # \u8f93\u51fa\uff1a15\nprint(sum_digits(987))  # \u8f93\u51fa\uff1a24<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f \u4f60\u5728\u5b66\u4e60\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\u300a\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01 \u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c \u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u91c7\u7528\u4ee5\u4e0b\u7b97\u6cd5\uff1a \u4f7f\u7528\u6c42\u4f59\u8fd0\u7b97\u7b26\uff08%\uff09\u63d0\u53d6\u6574\u6570\u7684\u672b\u5c3e\u6570\u5b57\u3002 \u4f7f\u7528\u53cc\u659c\u6760\u8fd0\u7b97\u7b26\uff08\/\/\uff09\u53bb\u9664\u672b\u5c3e\u6570\u5b57\uff0c\u66f4\u65b0\u6574\u6570\u3002 \u91cd\u590d\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u76f4\u5230\u6574\u6570\u53d8\u4e3a\u96f6\u3002 \u5b9e\u73b0\u8fd9\u6bb5\u7b97\u6cd5\u7684\u4ee3\u7801\u5982\u4e0b\uff1a def sum_digits(num): sum = 0 while num &gt; 0: sum += num % 10 num \/\/= 10 return sum \u5728\u51fd\u6570 sum_digits \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u91cd\u590d\u6267\u884c\u7b97\u6cd5\u6b65\u9aa4\uff0c\u76f4\u5230 num \u53d8\u6210\u96f6\u3002\u5faa\u73af\u7684\u6761\u4ef6\u662f num &gt; 0\uff0c\u8fd9\u786e\u4fdd\u4e86\u6211\u4eec\u53ea\u4f1a\u63d0\u53d6\u6574\u6570\u4e2d\u7684\u6570\u5b57\uff0c\u76f4\u5230\u5b83\u53d8\u4e3a\u7a7a\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u6765\u6d4b\u8bd5\u51fd\u6570\uff1a print(sum_digits(123)) # \u8f93\u51fa\uff1a6 print(sum_digits(456)) # \u8f93\u51fa\uff1a15 print(sum_digits(987)) # \u8f93\u51fa\uff1a24 \u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u5229\u7528\u6c42\u4f59\u548c\u6574\u9664\u8ba1\u7b97\u6574\u6570\u5404\u6570\u5b57\u4e4b\u548c\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-50137","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=50137"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/50137\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=50137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=50137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=50137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}