{"id":184351,"date":"2022-04-20T21:52:13","date_gmt":"2022-04-20T13:52:13","guid":{"rendered":"http:\/\/www.idc.net\/help\/184351\/"},"modified":"2022-04-20T21:52:13","modified_gmt":"2022-04-20T13:52:13","slug":"python-%e7%a8%8b%e5%ba%8f%ef%bc%9a%e8%ae%a1%e7%ae%97-lcm","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/184351\/","title":{"rendered":"Python \u7a0b\u5e8f\uff1a\u8ba1\u7b97 LCM"},"content":{"rendered":"<p>\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1a<\/p>\n<p>\u5728\u8fd9\u4e2a\u7b80\u5355\u7684 python \u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230\u4e24\u4e2a\u6570\u5b57\u7684 lcm\u3002\u8fd9\u662f\u4e00\u4e2a\u6570\u5b57 python \u7a0b\u5e8f\u3002<\/p>\n<p>\u8981\u7406\u89e3\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u60a8\u5e94\u8be5\u4e86\u89e3\u4ee5\u4e0b Python \u7f16\u7a0b\u4e3b\u9898:<\/p>\n<ul>\n<li>Python \u51fd\u6570<\/li>\n<li>Python \u5faa\u73af\u7ed3\u6784<\/li>\n<li>Python \u51b3\u7b56\u8bed\u53e5<\/li>\n<\/ul>\n<h2>LCM \u662f\u4ec0\u4e48\u610f\u601d\uff1f<\/h2>\n<p>\u5728\u8fd9\u4e2a\u6570\u5b57 python \u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230 LCM \u3002\u5728\u7b97\u672f\u7406\u8bba\u4e2d\uff0c LCM \u88ab\u79f0\u4e3a\u6700\u4e0d\u5e38\u89c1\u7684\u500d\u6570\u3002\u610f\u601d\u662f\u6700\u5c0f\u7684\u6574\u6570\uff0c\u662f a \u548c b \u7684\u500d\u6570\uff0c\u4e5f\u53ef\u4ee5\u63cf\u8ff0\u4e3a\u6700\u5c0f\u7684\u6570\uff0c\u53ef\u4ee5\u88ab\u4e24\u4e2a\u6570\u6574\u9664\u3002\u4e24\u4e2a\u6570\u7684 Lcm \u7528 lcm( a\uff0cb) \u8868\u793a\u3002\u8ba9\u6211\u4eec\u4ee5 lcm(5\uff0c2) \u4e3a\u4f8b\uff0c5 \u7684\u500d\u6570\u662f 5\uff0c10\uff0c15\uff0c20\uff0c25....2 \u7684\u500d\u6570\u662f 2\uff0c4\uff0c6\uff0c8\uff0c10\uff0c12...\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u5f53\u6211\u4eec\u53d6 LCM \u65f6\uff0c\u6211\u4eec\u5f97\u5230\u7684\u7b54\u6848\u662f 10\u3002<\/p>\n<h2>\u5982\u4f55\u5728 python \u4e2d\u627e\u5230 LCM\uff1f<\/h2>\n<p>\u5728\u8fd9\u4e2a\u7b80\u5355\u7684 python \u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5e94\u7528\u8fd9\u4e2a\u903b\u8f91\u6765\u627e\u5230\u7ed9\u5b9a\u6570\u5b57\u7684 LCM \u3002\u6240\u4ee5\u6211\u4eec\u9996\u5148\u8981\u8bc6\u522b\u7684\u66f4\u5927\u7684\u6570\u5b57\u662f\u4f7f\u7528 python \u8bed\u8a00\u4e2d\u7684 if \u6761\u4ef6\uff0c\u8fd9\u610f\u5473\u7740\u5728\u524d\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u66f4\u5927\u7684\u6570\u5b57\u662f 5 \u3002\u73b0\u5728\uff0c\u5f53\u6211\u4eec\u4ece\u66f4\u5927\u7684\u4ef7\u503c\u5f00\u59cb\u65f6\uff0c\u6211\u4eec\u5e94\u7528\u4e86\u8fd9\u4e2a\u903b\u8f91\u3002\u6211\u4eec\u5e94\u7528\u5177\u6709\u66f4\u5927\u503c\u7684 mod \u8fd0\u7b97\u7b26\u548c x\uff0cy \u3002\u6211\u4eec\u5728 python \u4e2d\u8c03\u7528\u4e00\u4e2a\u7528\u6237\u5b9a\u4e49\u7684\u51fd\u6570\u6765\u8fdb\u884c Lcm \u8ba1\u7b97\u3002<\/p>\n<p>\u5982\u679c\u4e24\u4e2a\u6761\u4ef6\u90fd\u6ee1\u8db3\uff0c\u90a3\u4e48\u6211\u4eec\u6253\u5370 lcm \u4e3a\u66f4\u5927\u3002\u6216\u8005\u6211\u4eec\u7528 1 \u589e\u52a0\u66f4\u5927\u7684\u503c\uff0c\u5e76\u5728\u5faa\u73af\u7684\u540c\u65f6\u7ee7\u7eed\u8be5 python\uff0c\u76f4\u5230\u6211\u4eec\u5f97\u5230\u4e00\u4e2a\u53ef\u88ab x \u548c y \u6574\u9664\u7684\u503c\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u4ee5\u8fd9\u4e2a\u521d\u7ea7 python \u7a0b\u5e8f\u4e2d\u7684\u524d\u4e00\u4e2a\u4f8b\u5b50\u4e3a\u4f8b\u3002\u6211\u4eec\u4ece\u66f4\u5927\u7684 5 \u5f00\u59cb\uff0c\u7528 x \u548c y \u8fdb\u884c mod \u64cd\u4f5c\uff0c\u5b83\u5c06\u662f 5 mod 5 \u548c 5 mod 2 \u5b83\u5c06\u5728\u7b2c\u4e00\u4e2a\u4e2d\u6ee1\u8db3\uff0c\u4f46\u5728\u7b2c\u4e8c\u4e2a\u4e2d\u4e0d\u6ee1\u8db3 2\u3002\u6240\u4ee5\u6211\u4eec\u7528 1 \u589e\u52a0\u66f4\u5927\uff0c\u5b83\u5c06\u662f 6\u3002\u7136\u540e\u53cd\u590d 6 mod 5 \u548c 6 mod 2\uff0c\u76f4\u5230\u5927\u4e8e 10\u3002\u90a3\u4e48\u5c31\u6ee1\u8db3\u4e86\uff0c\u6211\u4eec\u6253\u5370 LCM \u4e3a 10 \u3002<\/p>\n<h3>\u7b97\u6cd5<\/h3>\n<p>STEP 1: \u4f7f\u7528 python \u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u8f93\u5165\u51fd\u6570\u63a5\u53d7\u7528\u6237\u7684\u8f93\u5165\u3002\u5e76\u4f7f\u7528 int() \u4fdd\u5b58\u4e24\u4e2a\u53d8\u91cf\u4e2d\u7684\u503c\u3002<\/p>\n<p>\u6b65\u9aa4 2: \u4ee5 num1 \u548c num2 \u4e3a\u53c2\u6570\u8c03\u7528\u529f\u80fd lcm\u3002\u5c06\u8fd9\u4e9b\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\uff0c\u5e76\u4f7f\u7528 python \u8bed\u8a00\u7684 print \u5c06\u8fd4\u56de\u7ed3\u679c\u6253\u5370\u4e3a LCM\u3002<\/p>\n<h4>\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570 lcm(x\uff0cy) :<\/h4>\n<p>\u6b65\u9aa4 1: \u68c0\u67e5 x \u662f\u5426\u5927\u4e8e Y \u3002<\/p>\n<p>\u6b65\u9aa4 2: \u5206\u914d\u5927= x \u3002<\/p>\n<p>\u6b65\u9aa4 3: \u5982\u679c\u6761\u4ef6\u4e3a\u5047\uff0c\u5219\u4f7f\u7528 else \u6307\u5b9a\u5927\u4e8e= y \u3002<\/p>\n<p>\u6b65\u9aa4 4: \u7528\u4e00\u6bb5\u65f6\u95f4\u5f00\u59cb\u5faa\u73af\u3002<\/p>\n<p>\u6b65\u9aa4 5: \u68c0\u67e5\u6761\u4ef6\u5927 mod x \u548c\u5927 mod y \u662f\u5426\u4e3a\u771f\u3002<\/p>\n<p>\u6b65\u9aa4 6: \u5982\u679c\u662f\u771f\u7684\uff0clcm \u66f4\u5927\uff0c\u5e76\u4e2d\u65ad\u5faa\u73af\u3002<\/p>\n<p>\u7b2c 7 \u6b65:\u5426\u5219\u9012\u589e 1 \uff0c\u5e76\u5c06\u7ed3\u679c\u8fd4\u56de\u7ed9\u51fd\u6570\u3002<\/p>\n<h2>Python \u6e90\u4ee3\u7801<\/h2>\n<pre><code>def lcm(x, y):  \n    if x &gt; y:  \n        greater = x  \n    else:  \n        greater = y  \n    while(True):  \n        if((greater % x == 0) and (greater % y == 0)):  \n           lcm = greater  \n           break  \n           greater += 1  \n    return lcm  \n\nnum1 = int(input(\"Enter first number: \"))  \nnum2 = int(input(\"Enter second number:\"))  \nprint(\"The L.C.M. is\", lcm(num1, num2)) \n<\/code><\/pre>\n<h2>\u8f93\u51fa<\/h2>\n<pre><code>Enter first number: 3\nEnter first number: 6\n\nThe L.C.M. is 6<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1a \u5728\u8fd9\u4e2a\u7b80\u5355\u7684 python \u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230\u4e24\u4e2a\u6570\u5b57\u7684 lcm\u3002\u8fd9\u662f\u4e00\u4e2a\u6570\u5b57 p [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-184351","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=184351"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/184351\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=184351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=184351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=184351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}