{"id":10639,"date":"2024-03-30T19:53:11","date_gmt":"2024-03-30T11:53:11","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=10639"},"modified":"2025-12-26T18:55:58","modified_gmt":"2025-12-26T10:55:58","slug":"a-sql-for-24-point-game","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2024\/03\/a-sql-for-24-point-game\/","title":{"rendered":"\u4f7f\u7528\u4e00\u6761MySQL SQL\u8bed\u53e5\u5b8c\u621024\u70b9\u8ba1\u7b97"},"content":{"rendered":"\n\n\n\n<p style=\"margin-top:0px\">\u6700\u8fd1\uff0c\u516c\u53f8\u7ec4\u7ec7\u4e86\u4e00\u4e2a24\u70b9SQL\u7f16\u7a0b\u7684\u6bd4\u8d5b\uff0c\u7b14\u8005\u662f\u4e3b\u529e\u65b9\uff0c\u4e5f\u662f\u8bc4\u59d4\u3002\u65e2\u7136\u662f\u505a\u8bc4\u59d4\uff0c\u81ea\u5df1\u4e5f\u5148\u6311\u6218\u4e86\u4e00\u4e0b\uff0c\u56e0\u4e3a\u5bf9MySQL\u66f4\u4e3a\u719f\u6089\uff0c\u6545\u9009\u62e9\u4e86MySQL\u4f5c\u4e3a\u7f16\u7a0bSQL\u3002\u5468\u672b\u82b1\u4e86\u4e00\u4e9b\u65f6\u95f4\u6311\u6218\u4e00\u4e0b\uff0c\u8fd9\u91cc\u8bb0\u5f55\u4e00\u4e0b\u81ea\u5df1\u7684\u89e3\u6cd5\u4ee5\u53ca\u601d\u8def\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"line-height:1.5\">\u6982\u8ff0<\/h3>\n\n\n\n<p>24\u70b9\u95ee\u9898\uff0c\u662f\u4e00\u4e2a\u6709\u8da3\u7684\u95ee\u9898\u3002\u4ed6\u7684\u6269\u5c55\u95ee\u9898\uff08\u5373\u628a\u724c\u6570\/\u8ba1\u7b97\u503c\u8fdb\u884c\u66f4\u6539\uff09\uff0c\u5f88\u53ef\u80fd\u4e5f\u662f\u4e00\u4e2a<a href=\"https:\/\/en.wikipedia.org\/wiki\/NP-completeness\">NP-\u5b8c\u5168\u95ee\u9898<\/a>\uff0c\u4ed6\u4e0e<a href=\"https:\/\/en.wikipedia.org\/wiki\/Subset_sum_problem\">subset sum problem<\/a>\u95ee\u9898\u6709\u4e00\u4e9b\u4e9b\u7c7b\u4f3c\u3002\u5982\u679c\u53c2\u8003<a href=\"https:\/\/en.wikipedia.org\/wiki\/Subset_sum_problem\">subset sum problem<\/a>\u95ee\u9898\u7684\u89e3\u6cd5\uff08\u4f8b\u5982\u505a\u4e00\u4e9b\u52a8\u6001\u4f18\u5316\u89e3\uff09\uff0c\u5219\u53ef\u4ee5\u5b9e\u73b0\u8fd8\u6bd4\u8f83\u4f18\u7684\u89e3\u3002<\/p>\n\n\n\n<p>\u4e0d\u8fc7\uff0c\u8fd9\u4e00\u6b21\u7684\u6bd4\u8d5b\uff0c\u662f\u8981\u6c42\u5728\u4e00\u6761SQL\u91cc\u9762\u5b9e\u73b0\uff0c\u5e76\u4e14\u9650\u5236\u4e86SQL\u957f\u5ea6\u4e3a10KB\uff0c\u6240\u4ee5\u5c31\u5927\u5927\u9650\u5236\u4e86\u5b9e\u73b0\u7684\u65b9\u5f0f\u3002\u4e0d\u8fc7\u6700\u4e3a\u76f4\u63a5\u7684\u4e24\u4e2a\u601d\u8def\u8fd8\u662f\uff0c\u201c\u66b4\u529b\u7684\u679a\u4e3e\u201d\u8ba1\u7b97\u548c\u201c\u9884\u8ba1\u7b97\u7ed3\u679c\u518d\u505a\u54c8\u5e0c\u6c42\u89e3\u201d\u3002\u5373\u4fbf\u5982\u6b64\uff0c\u5728\u5199SQL\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u662f\u9047\u5230\u4e86\u5982\u4e0b\u6311\u6218\u9700\u8981\u89e3\u51b3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u5355\u6761SQL\u8fdb\u884c\u66b4\u529b\u679a\u4e3e\u7684\u65f6\u5019\uff0c\u5982\u4f55\u5728\u6ca1\u6709for\/while\u7b49\u5faa\u73af\u63a7\u5236\uff0c\u5982\u4f55\u904d\u5386\u6240\u6709\u7684\u53ef\u80fd\u6027<\/li>\n\n\n\n<li>\u54c8\u5e0c\u6570\u7ec4\u7684\u7a7a\u95f4\u5360\u7528\u6bd4\u8f83\u5927\uff0c\u53ef\u80fd\u4f1a\u8d85\u8fc710KB\uff0c\u5982\u4f55\u53bb\u538b\u7f29\u6216\u8005\u51cf\u5c11\u9700\u8981\u6784\u5efa\u7684\u6570\u7ec4<\/li>\n<\/ul>\n\n\n\n<p>\u53e6\u5916\uff0c\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u6d89\u53ca\u5230\u6d6e\u70b9\u6570\u8ba1\u7b97\u3001\u9664\u6570\u4e3a\u96f6\u7b49\u95ee\u9898\u7684\u5904\u7406\uff0c\u4e5f\u662f\u975e\u5e38\u5bb9\u6613\u51fa\u9519\u7684\u3002<\/p>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u89d2\u5ea6\uff0c\u8fd9\u4e9b\uff0c\u4e5f\u662f\u8fd9\u9053\u9898\uff0c\u6709\u8da3\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u201c\u4e00\u6761SQL\u7b9724\u70b9\u201d\u7684\u9898\u76ee\u56de\u987e<\/h3>\n\n\n\n<p>\u8fd9\u6b21\u7684\u9898\u76ee\uff0c\u4e0e\u4e00\u822c\u610f\u4e49\u4e0a24\u70b9\u7565\u6709\u4e00\u4e9b\u4e0d\u540c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u9996\u5148\uff0c\u8981\u6c42\u4e00\u6761SQL\u5185\u5b8c\u6210\uff1b\u5bf9\u4e8e\u7a77\u4e3e\u3001\u54c8\u5e0c\u7684\u5b9e\u73b0\u672c\u8eab\u5c31\u6709\u6311\u6218\u4e86\u3002\u9700\u8981\u5bf9SQL\u6bd4\u8f83\u719f\u6089\uff0c\u5426\u5219\u5f88\u96be\u5199\u51fa\u6b63\u786e\u3001\u9ad8\u6027\u80fd\u7684SQL<\/li>\n\n\n\n<li>SQL\u5927\u5c0f\u9650\u5236\u4e3a10KB\uff0c\u6240\u4ee5\uff0c\u5e76\u4e0d\u80fd\u7b80\u5355\u7684\u7a77\u4e3e\uff0c\u7b80\u5355\u7684CASE WHEN 10KB\u80af\u5b9a\u662f\u4e0d\u591f\u7684<\/li>\n\n\n\n<li>4\u4e2a\u6570\u5b57\uff0c\u88ab\u9650\u5236\u4e3a1~10\uff0c\u800c\u4e0d\u662f13\uff0c\u6240\u4ee5\u641c\u7d22\u7a7a\u95f4\u662f\u76f8\u5bf9\u6765\u8bf4\u5c11\u4e86\u4e00\u4e9b\u7684\uff0c\u8ba910KB\u4ee5\u5185\u54c8\u5e0c\u6210\u4e3a\u53ef\u80fd<\/li>\n<\/ul>\n\n\n\n<!--more-->\n\n\n\n<p>\u8be6\u7ec6\u8d5b\u9898\uff1a<a href=\"https:\/\/www.ninedata.cloud\/sql_poker24\">\u53c2\u8003<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"429\" src=\"https:\/\/www.orczhou.com\/wp-content\/uploads\/2023\/12\/image-55-1024x429.png\" alt=\"\" class=\"wp-image-10668\" srcset=\"https:\/\/www.orczhou.com\/wp-content\/uploads\/2023\/12\/image-55-1024x429.png 1024w, https:\/\/www.orczhou.com\/wp-content\/uploads\/2023\/12\/image-55-300x126.png 300w, https:\/\/www.orczhou.com\/wp-content\/uploads\/2023\/12\/image-55-768x322.png 768w, https:\/\/www.orczhou.com\/wp-content\/uploads\/2023\/12\/image-55-1536x643.png 1536w, https:\/\/www.orczhou.com\/wp-content\/uploads\/2023\/12\/image-55-2048x858.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u521d\u59cb\u5316\u6570\u636e<\/h3>\n\n\n\n<p>4\u5f20\u724c\uff0c\u6bcf\u5f20\u724c\u53d6\u503c\u4e3a1~10\uff0c\u6240\u4ee5\u4e00\u517110000\u4e2d\u53ef\u80fd\uff0c\u4f7f\u7528SQL\u6784\u5efa\u5b58\u50a8\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">CREATE TABLE cards(\n    id int auto_increment primary key,\n    c1 int ,\n    c2 int, \n    c3 int, \n    c4 int\n);\n\nINSERT INTO cards(c1,c2,c3,c4) \n    WITH RECURSIVE seq(n) as \n    (\n        select 1\n        union \n        select n+1 from seq where n&lt;=9\n    )\n    select t_1.n,t_2.n,t_3.n,t_4.n\n    from \n        seq as t_1,\n        seq as t_2,\n        seq as t_3,\n        seq as t_4<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6b21\u4e00\u5171\u5b9e\u73b0\u4e86\u4e24\u79cd\u7b97\u6cd5\uff0c\u4e00\u4e2a\u662f\u6b63\u7edf\u7684\u679a\u4e3e\u8ba1\u7b97\uff0c\u4e00\u4e2a\u662f\u7ed3\u679c\u5012\u63a8\u7684\u54c8\u5e0c\u89e3\u6cd5\u3002\u6211\u4eec\u5148\u770b\u770b\u5982\u4f55\u4f7f\u7528\u4e00\u6761SQL\u5b9e\u73b0\u6b63\u7edf\u7684\u679a\u4e3e\u8ba1\u7b97\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e00\u6761SQL\u7684\u6b63\u7edf\uff08\u201c\u66b4\u529b\u201d\uff09\u679a\u4e3e\u8ba1\u7b97<\/h3>\n\n\n\n<p>\u5b8c\u6574\u7684SQL\u53c2\u8003\uff1a<a href=\"https:\/\/www.orczhou.com\/24.v1.txt\">https:\/\/www.orczhou.com\/24.v1.txt<\/a> \u3002\u5982\u679c\u5bf9\u8fd9\u6761SQL\u6bd4\u8f83\u56f0\u60d1\u7684\u8bdd\uff0c\u53c8\u5bf9\u8fd9\u4e2a\u95ee\u9898\u6709\u5174\u8da3\u7684\u8bdd\uff0c\u53ef\u4ee5\u7ee7\u7eed\u9605\u8bfb\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u9898\u601d\u8def\u8bf4\u660e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528<a href=\"https:\/\/en.wikipedia.org\/wiki\/Binary_expression_tree\">\u4e8c\u53c9\u6811\u8868\u8fbe\u8868\u8fbe\u5f0f<\/a>\u3002\u679a\u4e3e\u7684\u641c\u7d22\u7a7a\u95f4\u8fd8\u662f\u975e\u5e38\u5927\u7684\uff0c\u5982\u679c\u4f7f\u7528\u4e8c\u53c9\u6811\u6765\u8868\u793a24\u70b9\u8ba1\u7b97\u7ed3\u679c\u7684\u8bdd\uff0c\u5b8c\u6574\u7684\u4f1a\u6709\u4e94\u79cd\u5f62\u5f0f\u7684\u6811\uff1aleft-most\u3001right-most\u548c3\u79cdbushy\u7684\u6811\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528JOIN\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u679a\u4e3e\u3002\u4f8b\u5982\uff0c\u8981\u679a\u4e3e\u6240\u6709\u7684\u4e09\u4e2a\u8fd0\u7b97\u7b26\uff0c\u6bcf\u4e2a\u8fd0\u7b97\u7b26\u6709\u56db\u79cd\u53ef\u80fd\uff08&#8221;+-*\/&#8221;\uff09\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528\u4e00\u5f20\u8868\uff0c\u8be5\u8868\u5171\u4e09\u4e2a\u5b57\u6bb5\u201cop_1st\u3001op_2nd\u3001op_3rd\u201d\uff0c\u51714*4*4\u6761\u8bb0\u5f55\uff0c\u6bcf\u6761\u8bb0\u5f55\u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\u7684\u7ec4\u5408\u3002\u7136\u540e\u4f7f\u7528\u8be5\u8868\u53bb\u4e0e\u539f\uff08cards\uff09\u8fdb\u884cJOIN\u3002<\/li>\n\n\n\n<li>\u9700\u8981\u679a\u4e3e\u7684\u9664\u4e86\u4e0a\u9762\u63d0\u5230\u7684\u8fd0\u7b97\u7b26\uff0c\u8fd8\u6709\u56db\u4e2a\u6570\u5b57\u7684\u987a\u5e8f\uff0c\u4f8b\u5982\uff0c\u4e00\u6761cards\u8868\u7684\u8bb0\u5f55\u6709\u56db\u4e2a\u6570\u5b57\uff1ac1\u3001c2\u3001c3\u3001c4\uff1b\u90a3\u4e48\uff0c\u5728\u679a\u4e3e\u8868\u8fbe\u5f0f  (c1 \/ (c2 &#8211; c3) )- c4 \u65f6\uff0c(c2 \/ (c1 &#8211; c3) )- c4\u7b49\u60c5\u51b5\u4e5f\u9700\u8981\u8003\u8651\uff0c\u8fd9\u79cd\u60c5\u51b5\u7684\u6570\u91cf\u662f4*3*2 = 12\u79cd\u3002\u5b9e\u73b0\u7684\u65b9\u5f0f\uff0c\u4e0e\u4e0a\u9762\u64cd\u4f5c\u7b26\u679a\u4e3e\u7c7b\u4f3c\uff0c\u6784\u5efa\u4e00\u4e2a\u8868\uff0c\u4f8b\u5982\u53ebfull_order\uff0c\u628a\u6240\u6709\u53ef\u80fd\u5f97\u987a\u5e8f\u90fd\u679a\u4e3e\u4e00\u904d\uff0c\u7136\u540e\u4e0e\u539f\u8868\uff08cards\uff09\u8fdb\u884cJOIN\u3002\u5177\u4f53\u7684full_order\u8868\u6709\u56db\u4e2a\u5b57\u6bb5c_[1-4]\uff0c\u6bcf\u4e2a\u5b57\u6bb5\u53d6\u503c\u4e3a[1-4]\uff0c\u4e14\u4e24\u4e24\u4e0d\u540c\uff0c\u90a3\u4e48\u8fd9\u4e2a\u8868\u5c31\u4ee3\u8868\u4e86\u6240\u6709\u7684c1\u3001c2\u3001c3\u3001c4\u7684\u987a\u5e8f\u53ef\u80fd\u3002<\/li>\n\n\n\n<li>\u6709\u4e86\u4e0a\u9762\u4e09\u79cd\u5206\u6790\uff0c\u90a3\u4e48\u5bf9\u4e8e\u4e00\u7ec4\u6570\u5b57\uff0c\u6240\u6709\u9700\u8981\u679a\u4e3e\u7684\u53ef\u80fd\u6027\u662f\uff1a5\u68f5\u6811*4*4*4\u79cd\u8fd0\u7b97\u7b26\u7ec4\u5408*4*3*2\u79cd\u987a\u5e8f\u7ec4\u5408\uff0c\u53737680\u79cd\u7ec4\u5408\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e8c\u53c9\u6811\u8868\u8fbe\u5f0f\u5206\u6790<\/h4>\n\n\n\n<p>\u8fd9\u5927\u6982\u5f88\u591a\u4eba\u4f1a\u9047\u5230\u7684\u662f\u7b2c\u4e00\u4e2a\u201c\u96be\u201d\u9898\uff0c\u4e5f\u6ce8\u610f\u5230\u5f88\u591a\u4eba\u5728\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u867d\u7136\u80fd\u591f\u679a\u4e3e\u90e8\u5206\u8868\u8fbe\u5f0f\uff0c\u4f46\u662f\u975e\u5e38\u5bb9\u6613\u9057\u6f0f\u3002\u53e6\u5916\uff0c\u4e5f\u56e0\u4e3a\u641c\u7d22\u7a7a\u95f4\u5f88\u5927\uff0c\u6240\u4ee5\uff0c\u5b9e\u73b0\u7ec6\u8282\u4e0a\u4e5f\u5f88\u5bb9\u6613\u51fa\u9519\u3002\u8fd9\u91cc\u4f7f\u7528\u57fa\u7840\u7684\u7f16\u8bd1\u539f\u7406\u77e5\u8bc6\u53ef\u4ee5\u77e5\u9053\uff0c\u4e00\u4e2a\u8868\u8fbe\u5f0f\u4e0e\u201c\u4e00\u79cd\u6811\u201d\u7ed3\u6784\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\uff0c\u800c\u8fd9\u6837\u7684\u6811\u4e00\u5171\u6709\u4e94\u79cd\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a ((c1*c2)+c3)*c4\u3002\u90a3\u4e48\u5b83\u5bf9\u5e94\u6811\u5f62\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">               ((c1*c2)+c3)*c4       ((c1 op_1st c2) op_2nd c3) op_3rd c4\n                     &lt;*&gt;                             &lt;op_1st&gt;\n                      |                                 |\n                ------------                      ------------\n                |          |                      |          |\n               &lt;+&gt;        c4                   &lt;op_2nd&gt;      c4\n                |                                 |\n         --------------                    --------------\n         |            |                    |            |\n        &lt;*&gt;           c3                &lt;op_3rd&gt;        c3\n         |                                 |\n   ------------                      ------------\n   |          |                      |          |\n  c1          c2                    c1          c2<\/code><\/pre>\n\n\n\n<p>\u90a3\u4e48\u5bf9\u4e8e\u4efb\u610f\u4e00\u7ec4\u6570\u5b57\uff08c1,c2,c3,c4\uff09\u4e00\u5171\u6709\u591a\u5c11\u79cd\u8fd9\u6837\u7684\u6811\u5462\uff1f\u7b54\u6848\u662f\u4e94\u79cd\uff0c\u8fd9\u91cc\u4e0d\u4e00\u4e00\u8be6\u8ff0\uff0c\u6bcf\u79cd\u6811\u5bf9\u5e94\u7684\u8868\u8fbe\u5f0f\u5982\u4e0b\uff0c\u8fd9\u4e86\u4f7f\u7528op_1\u3001op_2\u3001op_3\u4ee3\u8868\u201c+-*\/\u201d\u4e2d\u7684\u4efb\u610f\u4e00\u79cd\u8fd0\u7b97\u7b26\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>((c1 op_1 c2) op_2 c3) op_3 c4  \u5373\u4e0a\u9762\u7684\u5de6\u6df1\u6811<\/li>\n\n\n\n<li>c1 op_1 (c2 op_2 (c3 op_3 c4))  \u53f3\u6df1\u6811<\/li>\n\n\n\n<li>(c1 op_1 c2) op_2 (c3 op_3 c4)  bushy\u6811<\/li>\n\n\n\n<li>c1 op_1 ((c2 op_2 c3) op_3 c4)  bushy\u6811<\/li>\n\n\n\n<li>(c1 op_1 (c2 op_2 c3)) op_3 c4  bushy\u6811<\/li>\n<\/ul>\n\n\n\n<p>\u5927\u5bb6\u53ef\u4ee5\u7528\u4e0a\u9762\u7684\u6811\u5f62\u56fe\u753b\u4e00\u4e0b\u4e94\u79cd\u6811\uff0c\u5c31\u6bd4\u8f83\u597d\u7406\u89e3\u4e86\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6bcf\u79cd\u6811\u7684\u53ef\u80fd\u6027\u679a\u4e3e<\/h4>\n\n\n\n<p>\u5bf9\u4e8e\u4e0a\u8ff0\u7684\u6bcf\u4e00\u68f5\u6811\uff0c\u90fd\u6709\u4e09\u4e2a\u201c\u64cd\u4f5c\u7b26\u201d\u548c\u201c\u56db\u4e2a\u64cd\u4f5c\u6570\u201d\uff0c\u8fd9\u4e09\u4e2a\u64cd\u4f5c\u7b26\u90fd\u67094\u4e2d\u9009\u62e9\uff08\u201c+-*\/\u201d\uff09\uff0c\u56db\u4e2a\u64cd\u4f5c\u6570\u7684\u9009\u62e9\u7a7a\u95f4\u8981\u5c0f\u4e00\u4e9b\uff0c\u56e0\u4e3a\u4e0d\u80fd\u91cd\u590d\uff0c\u4e0d\u8fc7\u6839\u636e\u7b80\u5355\u7684\u6392\u5217\u7ec4\u5408\u77e5\u8bc6\u53ef\u4ee5\u6709\uff1a<code>(4*4*4)*(4*3*2*1)<\/code>\u79cd\u53ef\u80fd\u6027\u3002<\/p>\n\n\n\n<p>\u518d\u4e0e\u4e0a\u9762\u76845\u79cd\u6811\u7ec4\u5408\uff0c\u4e00\u5171\u6709 <code>5*(4*4*4)*(4*3*2*1)=7680<\/code>\u79cd\u7ec4\u5408\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u91cd\u590d\u7684\u6811<\/h4>\n\n\n\n<p>\u8fd9\u91cc\u7684\u6811\u7684\u79cd\u7c7b\u770b\u8d77\u6765\u975e\u5e38\u591a\uff0c\u4f46\u662f\u56e0\u4e3a\u52a0\u6cd5\u548c\u4e58\u6cd5\u6709\u4ea4\u6362\u5f8b\u3001\u7ed3\u5408\u5f8b\uff0c\u4ee5\u53ca\u51cf\u6cd5\u6709\u53bb\u62ec\u53f7\u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\uff0c\u201c\u7b49\u4ef7\u7684\u6811\u201d\u975e\u5e38\u591a\u3002\u53bb\u6389\u7b49\u4ef7\u7684\u6811\uff0c\u80fd\u591f\u628a\u8fd9\u4e2a\u641c\u7d22\u7a7a\u95f4\u5927\u5e45\u5ea6\u7f29\u5c0f\u3002\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff1a\u7406\u8bba\u4e0a\uff0c\u53bb\u6389\u6240\u6709\u91cd\u590d\uff08\u201c\u7b49\u4ef7\u201d\uff09\u7684\u6811\uff0c\u6700\u540e\u5269\u4f59\u7684\u6570\u91cf\u662f\u591a\u5c11\uff1f(\u8fd9\u4f3c\u4e4e\u5e76\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u9898\uff0c\u4e0d\u8fc7\u4e0d\u5c5e\u4e8e\u672c\u6587\u8ba8\u8bba\u7684\u5185\u5bb9\uff09\u3002<\/p>\n\n\n\n<p>\u5728\u5f88\u591a\u7684\u7b97\u6cd5\u4f18\u5316\u91cc\u9762\uff0c\u5982\u679c\u80fd\u591f\u5c3d\u53ef\u80fd\u591a\u7684\u628a\u8fd9\u4e9b\u201c\u7b49\u4ef7\u201d\u6811\u780d\u6389\uff0c\u5c31\u53ef\u4ee5\u5927\u5927\u63d0\u5347\u6267\u884c\u7684\u6548\u7387\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u6b21\u89e3\u9898\u4e2d\uff0c\u516c\u53f8\u6709\u4e2a\u540c\u4e8b\u6bd4\u8f83\u6781\u9650\uff0c\u5728\u4e0a\u9762\u7684\u95ee\u9898\u4e2d\uff0c\u628a\u8fd9\u4e9b\u6811\u7684\u679a\u4e3e\u53ef\u80fd\u6027\u780d\u5230\u4e86\u975e\u5e38\u5c0f\u3002\u5f53\u7136\uff0c\u56e0\u4e3a\u662f\u9650\u5236\u5728\u8fd9\u9053\u9898\u4e2d\uff0c\u5f88\u591a\u6811\u53ef\u80fd\u662f\u65e0\u6548\u7684\uff08\u867d\u7136\u6ca1\u6709\u7b49\u4ef7\u6811\uff0c\u4f46\u662f\u53ef\u80fd\u8ba1\u7b97\u4e2d\u5e76\u4e0d\u9700\u8981\u4f7f\u7528\uff09\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u7684\uff0c\u7b49\u4ef7\u7684\u6811\u5305\u62ec\u4e86\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u52a0\u6cd5\u3001\u4e58\u6cd5\u7684\u4ea4\u6362\u5f8b\u4f1a\u5bfc\u81f4\u5927\u91cf\u7684\u91cd\u590d\u6811<\/li>\n\n\n\n<li>\u52a0\u6cd5\u3001\u4e58\u6cd5\u7684\u7ed3\u5408\u5f8b\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u5f88\u591a\u7684\u91cd\u590d\u7684\u6811<\/li>\n\n\n\n<li>\u51cf\u6cd5\u548c\u9664\u6cd5\u7684\u53bb\u62ec\u53f7\u7b49\u4ef7\u53d8\u5316\uff08\u4f8b\u5982c1-(c2-c3)\u4e0ec1-c2+c3\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u5728\u8fd9\u91cc\u7684\u4e2d\uff0c\u6682\u65f6\u6ca1\u6709\u8003\u8651\u8fd9\u4e9b\u7b49\u4ef7\u6811\u7684\u6d88\u9664\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u64cd\u4f5c\u7b26\u7684\u904d\u5386SQL\u5b9e\u73b0<\/h4>\n\n\n\n<p>\u5982\u524d\u6240\u8ff0\uff0c\u6bcf\u9897\u6811\u5171\u6709\u4e09\u4e2a\u64cd\u4f5c\u7b26\uff0c\u90fd\u53ef\u4ee5\u662f\u201c+-*\/\u201d\u4e2d\u7684\u4efb\u4f55\u4e00\u4e2a\uff0c\u8fd9\u91cc\u4f7f\u7528MySQL\u7684<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/with.html\">CTE<\/a>\uff08WITH\/Common Table Expressions\uff09\u529f\u80fd\u548cJOIN\u529f\u80fd\u5b9e\u73b0\u679a\u4e3e\u548c\u904d\u5386\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">(\n    WITH op_list (op) as (\n        SELECT '*'\n        UNION\n        SELECT '+'\n        UNION\n        SELECT '-'\n        UNION\n        SELECT '\/'\n    )\n    SELECT\n        op_1.op as op_1st,\n        op_2.op as op_2nd,\n        op_3.op as op_3rd\n    FROM\n        op_list as op_1,\n        op_list as op_2,\n        op_list as op_3\n) full_op<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u201c\u64cd\u4f5c\u6570\u201d\u987a\u5e8f\u7684\u679a\u4e3e<\/h4>\n\n\n\n<p>\u6bcf\u4e00\u9897\u6811\u90fd\u6709\u56db\u4e2a\u201c\u64cd\u4f5c\u6570\u201d\uff0c\u6bcf\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f{c1,c2,c3,c4}\u4e2d\u7684\u4e00\u4e2a\uff0c\u4f46\u4e0d\u91cd\u590d\uff08\u8fd9\u91cc\u7684\u4e0d\u91cd\u590d\u662f\u6307\u4e0d\u80fd\u51fa\u73b0c1 c1 c3 c4\u8fd9\u56db\u4e2a\u6570\u5b57\u6bcf\u4e2a\u7528\u4e00\u904d\uff0c\u4f46\u9700\u8981\u6ce8\u610fc1 c2 c3 c4\u672c\u8eab\u662f\u53ef\u80fd\u6709\u91cd\u590d\u7684\u6570\u5b57\u7684\uff0c\u4f8b\u5982 3,3,5,8\u7684\u6570\u5b57\u7ec4\u5408\uff09\u3002\u73b0\u5728\u9700\u8981\u628a\u56db\u4e2a\u64cd\u4f5c\u6570\u7684\u6240\u6709\u7ec4\u5408\uff084*3*2\u79cd\uff09\u5168\u90e8\u90fd\u679a\u4e3e\u51fa\u6765\u3002\u8fd9\u91cc\u4f7f\u7528\u884c\u8f6c\u5217\u540e\uff0c\u518d\u4f7f\u75284\u4e2a\u987a\u5e8f\u8868\u7684\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<p>\u4e3a\u4e86\u5b9e\u73b04\u4e2a\u64cd\u4f5c\u7684\u4e0d\u91cd\u590d\u7684\u7ec4\u5408\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">          (\n              WITH RECURSIVE seq (n) as (\n              SELECT 1\n              UNION ALL\n              SELECT n + 1 FROM seq WHERE n &lt;= 3\n          )\n          select\n              seq_1.n as seq_num_1,\n              seq_2.n as seq_num_2,\n              seq_3.n as seq_num_3,\n              seq_4.n as seq_num_4\n          from\n              seq as seq_1,\n              seq as seq_2,\n              seq as seq_3,\n              seq as seq_4\n          WHERE\n              pow(2,seq_1.n-1)+pow(2,seq_2.n-1)+pow(2,seq_3.n-1)+pow(2,seq_4.n-1) = 15\n          ) full_order<\/code><\/pre>\n\n\n\n<p>\u5230\u8fd9\u91cc\uff0cfull_order\u8868\u5c31\u53ef\u4ee5\u8868\u793a\u6240\u6709\u7684\u6392\u5217\u7ec4\u5408\u4e86\u3002\u4f46\u662f\u5982\u4f55\u5229\u7528full_order\u8868\u7684\u56db\u4e2a\u5217seq_1\u3001seq_2\u3001seq_3\u3001seq_4\u6765\u628a{c1,c2,c3,c3}\u90fd\u679a\u4e3e\u51fa\u6765\uff0c\u8fd8\u9700\u8981\u505a\u4e00\u4e9b\u8f6c\u6362\u3002\u8fd9\u4e2a\u8f6c\u6362\u8981\u5728SELECT\u4e2d\u7684item list\u90e8\u5206\u3002\u5373\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">SELECT \n    item_list\nFROM \n    cards,\n    (...) as full_order\n    (...) as full_op<\/code><\/pre>\n\n\n\n<p>\u5728iteml_list\u90e8\u5206\uff0c\u9700\u8981\u5bf9c1,c2,c3,c4\u6309\u7167full_order\u8fdb\u884c\u91cd\u65b0\u6392\u5e8f\u5904\u7406\uff0c\u8fd9\u91cc\u662f\u7565\u6709\u4e00\u4e9b\u590d\u6742\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">SELECT \n        ...\n        @c_1 := case full_order.seq_num_1 \n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_1,\n        @c_2 := case full_order.seq_num_2\n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_2,\n        @c_3 := case full_order.seq_num_3\n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_3,\n        @c_4 := case full_order.seq_num_4\n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_4,\n        ...\nFROM \n    cards,\n    (...) as full_order\n    (...) as full_op<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e94\u79cd\u201c\u8868\u8fbe\u5f0f\u6811\u201d\u7684\u8ba1\u7b97<\/h4>\n\n\n\n<p>\u5728\u524d\u9762\u7684\u5c0f\u7ed3\u201c\u4e8c\u53c9\u6811\u8868\u8fbe\u5f0f\u5206\u6790\u201d\u4e2d\uff0c\u5df2\u7ecf\u5bf9\u4e94\u79cd\u8868\u8fbe\u5f0f\u8fdb\u884c\u4e86\u5206\u6790\u3002\u5bf9\u4e8e\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u7684\u201c\u64cd\u4f5c\u7b26\u201d\u3001\u201c\u64cd\u4f5c\u6570\u201d\u4e5f\u5df2\u7ecf\u51c6\u5907\u597d\u4e86\u3002\u90a3\u4e48\u5c31\u9700\u8981\u9010\u4e00\u8ba1\u7b975\u4e2d\u8868\u8fbe\u5f0f\u4e86\u3002\u8fd9\u91cc\u4e5f\u662f\u7528\u6700\u201c\u66b4\u529b\u201d\u7684\u65b9\u5f0f\uff0c\u5206\u522b\u8ba1\u7b97\u4e94\u68f5\u6811\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4ec5\u6682\u65f6left most tree\u7684\u8ba1\u7b97\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">        \/* total 5 trees *\/   \n        \/*left most tree*\/\n        \/* ((@c_1 op_1 @c_2) op_2 @c_3) op_3 @c_4  *\/\n        @lt_1 := case op_1st \n            when '*' then @c_1 * @c_2\n            when '+' then @c_1 + @c_2\n            when '-' then @c_1 - @c_2\n            when '\/' then @c_1 \/ @c_2\n        END as lt_1,\n        \n        @lt_2 := case op_2nd \n            when '*' then @lt_1 * @c_3\n            when '+' then @lt_1 + @c_3\n            when '-' then @lt_1 - @c_3\n            when '\/' then @lt_1 \/ @c_3\n        END as lt_2,\n        \n        @lt_3 := case op_3rd \n            when '*' then @lt_2 * @c_4\n            when '+' then @lt_2 + @c_4\n            when '-' then @lt_2 - @c_4\n            when '\/' then @lt_2 \/ @c_4\n        END as lt_3,\n        \n        @lt_expr := concat(\"((\", @c_1 ,op_1st,@c_2 ,\")\",op_2nd,@c_3,\")\",op_3rd,@c_4),\n        if(@lt_3 between 24-0.0001 and 24+0.0001, @if_found := true, 0),\n        if(@lt_3 between 24-0.0001 and 24+0.0001, @r_expr := @lt_expr, 0),<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6d6e\u70b9\u6570\u7684\u7cbe\u5ea6\u4e0e\u9664\u6570\u4e3a\u96f6\u7684\u95ee\u9898<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e24\u4e2a\u95ee\u9898\u9700\u8981\u6ce8\u610f\uff0c\u4e5f\u662f\u5728\u6574\u4e2a\u6bd4\u8d5b\u8fc7\u7a0b\u4e2d\uff0c\u5f88\u591a\u9009\u624b\u90fd\u4f1a\u72af\u9519\u8bef\u7684\u5730\u65b9\uff0c\u5176\u4e2d\u4e00\u4e2a\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6d6e\u70b9\u6570\u7cbe\u5ea6\u7684\u95ee\u9898<\/li>\n<\/ul>\n\n\n\n<p>\u5728\u5f88\u591a\u7b97\u5f0f\u7684\u8ba1\u7b97\u4e2d\u4f1a\u6d89\u53ca\u5230\u201c\u65e0\u9650\u5faa\u73af\u5c0f\u6570\u201d\uff0c\u800c\u8ba1\u7b97\u673a\u5728\u5904\u7406\u65f6\uff0c\u5219\u4f1a\u901a\u8fc7\u6309\u7167\u4e00\u5b9a\u7684\u7cbe\u5ea6\u8fd1\u4f3c\u3002\u4f8b\u59823\u30013\u30018\u30018\u7684\u8ba1\u7b97\u65b9\u6cd58\/(3-8\/3)\u3002\u8fd9\u4e2a\u95ee\u9898\u6bd4\u770b\u8d77\u6765\u7684\u66f4\u52a0\u9690\u853d\uff0c\u5728MySQL\u4e2d\uff0c\u6211\u4eec\u89c2\u5bdf\u5982\u4e0b\u8868\u8fbe\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; select 8\/(3-8\/3),@i:=8\/3,@j:=3-@i,@k:=8\/@j;\n+-----------+---------+-------------+--------------------+\n| 8\/(3-8\/3) | @i:=8\/3 | @j:=3-@i    | @k:=8\/@j           |\n+-----------+---------+-------------+--------------------+\n|   24.0000 |  2.6667 | 0.333333334 | 23.999999952000003 |\n+-----------+---------+-------------+--------------------+<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u76f4\u63a5\u7684\u8ba1\u7b97<code>8\/(3-8\/3)<\/code>\u662f\u53ef\u4ee5\u7b97\u51fa24\u7684\uff0c\u4f46\u5206\u6b65\u9aa4\u8ba1\u7b97\uff0c\u5219\u4f1a\u51fa\u9519\uff0c\u6240\u4ee5\uff0c\u5728\u5b9e\u73b0\u65f6\uff0c\u5982\u679c\u662f\u5206\u6b65\u8ba1\u7b97\uff0c\u5219\u5f88\u5bb9\u6613\u4f1a\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n\n\n\n<p>\u77e5\u9053\u4e86\u9519\u8bef\u5728\u54ea\u91cc\uff0c\u89e3\u51b3\u5176\u5b9e\u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u5728\u6700\u7ec8\u7684\u8ba1\u7b97\u7ed3\u679c\u505a\u4e00\u6b21\u56db\u820d\u4e94\u5165\uff0c\u4f8b\u5982\u4fdd\u75593\u4f4d\u5c0f\u6570\u5373\u53ef\uff0c\u5373\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; select 8\/(3-8\/3),@i:=8\/3,@j:=3-@i,@k:=round(8\/@j,4);\n+-----------+---------+-------------+-------------------+\n| 8\/(3-8\/3) | @i:=8\/3 | @j:=3-@i    | @k:=round(8\/@j,4) |\n+-----------+---------+-------------+-------------------+\n|   24.0000 |  2.6667 | 0.333333334 |           24.0000 |\n+-----------+---------+-------------+-------------------+<\/code><\/pre>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u5728\u7ed3\u679c\u5224\u65ad\u7684\u65f6\u5019\uff0c\u518d\u5f15\u5165\u4e00\u6b21\u989d\u5916\u7684\u6bd4\u8f83\u5373\u53ef\u3002\u53ef\u4ee5\u770b\u4e0b\u9762\u7684SQL\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; select 8\/(3-8\/3),@i:=8\/3,@j:=3-@i,@k:=8\/@j,@k = 24,@k between 24-0.0001 and 24+0.0001\\G\n*************************** 1. row ***************************\n                         8\/(3-8\/3): 24.0000\n                           @i:=8\/3: 2.6667\n                          @j:=3-@i: 0.3333333340000002\n                          @k:=8\/@j: 23.999999951999985\n                           @k = 24: 0\n@k between 24-0.0001 and 24+0.0001: 1<\/code><\/pre>\n\n\n\n<p>\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\u201c\u9664\u6570\u4e3a\u96f6\u7684\u95ee\u9898\u201d\uff0c\u8fd9\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u8003\u8651\u5230\uff0c\u4f46\u53ef\u80fd\u65e0\u9700\u505a\u989d\u5916\u7684\u5904\u7406\u3002\u5728\u7a77\u4e3e\u7684\u7b97\u6cd5\u4e2d\uff0c\u6709\u5f88\u591a\u662f\u9700\u8981\u9664\u4ee50\u7684\u3002\u5728MySQL\u4e2d\uff0c\u5982\u679cSELECT\u8bed\u53e5\u7684\u8bdd\uff0c\u9664\u4ee5\u96f6\u7684\u8868\u8fbe\u5f0f\u4f1a\u8fd4\u56deNULL\u3002\u5728\u5904\u7406\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u8fd9\u4e2a\u7ec6\u8282\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u7684\uff0c\u53ef\u4ee5\u53c2\u8003MySQL\u7684\u6587\u6863\uff08<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/sql-mode.html#sqlmode_error_for_division_by_zero\">\u53c2\u8003<\/a>\uff09\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>For SELECT, division by zero returns NULL. Enabling ERROR_FOR_DIVISION_BY_ZERO causes a warning to be produced as well, regardless of whether strict mode is enabled.<\/p>\n<cite><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/sql-mode.html#sqlmode_error_for_division_by_zero\">\u53c2\u8003<\/a><\/cite><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd4\u56de\u8868\u8fbe\u5f0f<\/h3>\n\n\n\n<p>\u6700\u540e\uff0c\u5bf9\u4e8e\u4e00\u7ec4\u6570\u636e\uff0c\u7b97\u51fa\u6240\u6709\u4e94\u68f5\u6811\u7684\u53d6\u503c\u540e\uff0c\u6700\u540e\u770b\u770b\u6709\u6ca1\u6709\u7b49\u4e8e24\u7684\uff0c\u6216\u8005\u5176\u4e2d\u4e4b\u4e00\u7b49\u4e8e24\uff0c\u5c31\u53ef\u4ee5\u505c\u6b62\u8ba1\u7b97\u4e86\uff0c\u540c\u65f6\u9700\u8981\u5c06\u8be5\u6811\u6240\u4ee3\u8868\u7684\u8868\u8fbe\u5f0f\u8f93\u51fa\u51fa\u6765\uff0c\u4ee5\u4f9b\u540e\u7eed\u4f7f\u7528\u3002\u4f8b\u5982\u5bf9\u4e8e\u524d\u9762\u7684left-most tree\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">        \/* total 5 trees *\/   \n        \/*left most tree*\/\n        \/* ((@c_1 op_1 @c_2) op_2 @c_3) op_3 @c_4  *\/\n        @lt_1 := case op_1st \n            when '*' then @c_1 * @c_2\n            when '+' then @c_1 + @c_2\n            when '-' then @c_1 - @c_2\n            when '\/' then @c_1 \/ @c_2\n        END as lt_1,\n        \n        @lt_2 := case op_2nd \n            when '*' then @lt_1 * @c_3\n            when '+' then @lt_1 + @c_3\n            when '-' then @lt_1 - @c_3\n            when '\/' then @lt_1 \/ @c_3\n        END as lt_2,\n        \n        @lt_3 := case op_3rd \n            when '*' then @lt_2 * @c_4\n            when '+' then @lt_2 + @c_4\n            when '-' then @lt_2 - @c_4\n            when '\/' then @lt_2 \/ @c_4\n        END as lt_3,\n        \n        @lt_expr := concat(\"((\", @c_1 ,op_1st,@c_2 ,\")\",op_2nd,@c_3,\")\",op_3rd,@c_4),\n        if(@lt_3 between 24-0.0001 and 24+0.0001, @if_found := true, 0),\n        if(@lt_3 between 24-0.0001 and 24+0.0001, @r_expr := @lt_expr, 0),<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6700\u540e\uff0c\u7ec4\u88c5\u9700\u8981\u7684\u8f93\u51fa\u7684\u5217<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u6ca1\u6709\u4ec0\u4e48\u7279\u522b\u9700\u8981\u5f3a\u8c03\u7684\uff0c\u6700\u540e\u6309\u7167\u9898\u76ee\u4e2d\u8981\u6c42\u7684\uff0c\u8f93\u51fa\u9700\u8981\u7684\u5217\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>\u5b8c\u6574\u7684SQL\u53c2\u8003\uff1a<a href=\"https:\/\/www.orczhou.com\/24.v1.txt\">https:\/\/www.orczhou.com\/24.v1.txt<\/a> \u3002<\/p>\n\n\n\n<p>\u5b8c\u6574\u7684SQL\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">-- more about the SQL:\n-- https:\/\/www.orczhou.com\/index.php\/2024\/03\/a-sql-for-24-point-game\/\n select id,t.c1,t.c2,t.c3,t.c4,\n    (\n    select result_expr\n    FROM\n    (\n        select\n\n        @if_found := false,\n        @r_expr := 'failed',\n\n        t_each_row.id,c1,c2,c3,c4, op_1st,op_2nd,op_3rd,\n        @c_1 := case full_order.seq_num_1 \n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_1,\n        @c_2 := case full_order.seq_num_2\n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_2,\n        @c_3 := case full_order.seq_num_3\n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_3,\n        @c_4 := case full_order.seq_num_4\n            when 1 then c1 \n            when 2 then c2 \n            when 3 then c3 \n            when 4 then c4 \n        END as c_4,\n--    , @c_1,@c_2,@c_3,@c_4,\n    \n    \n        \/* total 5 trees *\/   \n        \/*left most tree*\/\n        \/* ((@c_1 op_1 @c_2) op_2 @c_3) op_3 @c_4  *\/\n        @lt_1 := case op_1st \n            when '*' then @c_1 * @c_2\n            when '+' then @c_1 + @c_2\n            when '-' then @c_1 - @c_2\n            when '\/' then @c_1 \/ @c_2\n        END as lt_1,\n        \n        @lt_2 := case op_2nd \n            when '*' then @lt_1 * @c_3\n            when '+' then @lt_1 + @c_3\n            when '-' then @lt_1 - @c_3\n            when '\/' then @lt_1 \/ @c_3\n        END as lt_2,\n        \n        @lt_3 := case op_3rd \n            when '*' then @lt_2 * @c_4\n            when '+' then @lt_2 + @c_4\n            when '-' then @lt_2 - @c_4\n            when '\/' then @lt_2 \/ @c_4\n        END as lt_3,\n        \n        @lt_expr := concat(\"((\", @c_1 ,op_1st,@c_2 ,\")\",op_2nd,@c_3,\")\",op_3rd,@c_4),\n        if(@lt_3 between 24-0.0001 and 24+0.0001, @if_found := true, 0),\n        if(@lt_3 between 24-0.0001 and 24+0.0001, @r_expr := @lt_expr, 0),\n        \n        \/* bushy tree 00 *\/\n        \/* (c1 op_1st c2) op_2nd (c3 op_3rd c4)  *\/\n        if(\n            @if_found = false,\n            @bt_1 := case op_1st\n                when '*' then @c_1 * @c_2\n                when '+' then @c_1 + @c_2\n                when '-' then @c_1 - @c_2\n                when '\/' then @c_1 \/ @c_2\n            END,\n            0\n            ) as bt_1,\n        \n        if(\n            @if_found = false,\n            @bt_2 := case op_3rd\n                when '*' then @c_3 * @c_4\n                when '+' then @c_3 + @c_4\n                when '-' then @c_3 - @c_4\n                when '\/' then @c_3 \/ @c_4\n            END,\n            0\n            ) as bt_2,\n    \n        if(\n            @if_found = false,\n            @bt_3 := case op_2nd\n                \/* '+' &amp; '*' there is always a equel tree   *\/\n                when '*' then @bt_1 * @bt_2\n                when '+' then @bt_1 + @bt_2\n                when '-' then @bt_1 - @bt_2\n                when '\/' then @bt_1 \/ @bt_2\n            END,\n            0\n            ) as bt_3,\n       \n        \n        @bt_expr := concat(\"(\",@c_1,op_1st,@c_2,\")\",op_2nd,\"(\",@c_3,op_3rd,@c_4,\")\"),\n        if(@bt_3 between 24-0.0001 and 24+0.0001, @if_found := true , 0),\n        if(@bt_3 between 24-0.0001 and 24+0.0001, @r_expr := @bt_expr, 0),\n   \n \n        \/*right most tree*\/\n        \/* c1 op_1 (c2 op_2 (c3 op_3 c4))  *\/\n        if(\n            @if_found = false,\n            @rt_1 := case op_3rd \n                when '*' then @c_3 * @c_4\n                when '+' then @c_3 + @c_4\n                when '-' then @c_3 - @c_4\n                when '\/' then @c_3 \/ @c_4\n            END,\n            0\n            ) as rt_1,\n    \n        \n        if(\n            @if_found = false,\n            @rt_2 := case op_2nd \n                when '*' then @c_2 * @rt_1  \n                when '+' then @c_2 + @rt_1  \n                when '-' then @c_2 - @rt_1  \n                when '\/' then @c_2 \/ @rt_1  \n            END,\n            0\n            ) as rt_2,\n        \n        if(\n            @if_found = false,\n            @rt_3 := case op_1st\n                when '*' then @c_1 * @rt_2\n                when '+' then @c_1 + @rt_2\n                when '-' then @c_1 - @rt_2\n                when '\/' then @c_1 \/ @rt_2\n            END,\n            0\n            ) as rt_3,\n    \n        \n        @rt_expr := concat(@c_1, op_1st, \"(\", @c_2 ,op_2nd, \"(\",@c_3, op_3rd, @c_4,\")\",\")\"),\n        if(@rt_3 between 24-0.0001 and 24+0.0001, @if_found := true, 0),\n        if(@rt_3 between 24-0.0001 and 24+0.0001, @r_expr := @rt_expr, 0),\n    \n        \/* bushy tree 01  *\/\n        \/* (c2 op2 (c3 op3 c4)) op1 c1  *\/\n        if(\n            @if_found = false,\n            @bt01_1 := case op_3rd\n                when '*' then @c_3 * @c_4\n                when '+' then @c_3 + @c_4\n                when '-' then @c_3 - @c_4\n                when '\/' then @c_3 \/ @c_4\n            END,\n            0\n            ) as bt01_1,\n        \n        if(\n            @if_found = false,\n            @bt01_2 := case op_2nd\n                when '*' then @c_2 * @bt01_1\n                when '+' then @c_2 + @bt01_1\n                when '-' then @c_2 - @bt01_1\n                when '\/' then @c_2 \/ @bt01_1\n            END,\n            0\n            ) as bt01_2,\n        \n        if(\n            @if_found = false,\n            @bt01_3 := case op_1st\n                \/* '+' &amp; '*' there is always a equel tree   *\/\n                when '*' then @bt01_2 * @c_1\n                when '+' then @bt01_2 + @c_1\n                when '-' then @bt01_2 - @c_1\n                when '\/' then @bt01_2 \/ @c_1\n            END,\n            0\n            ) as bt01_3,\n    \n        @bt01_expr := concat(\"(\",@c_2, op_2nd , \"(\" ,@c_3, op_3rd, @c_4, \"))\", op_1st, @c_1 ),\n        if(@bt01_3 between 24-0.0001 and 24+0.0001 , @if_found := true , 0),\n        if(@bt01_3 between 24-0.0001 and 24+0.0001 , @r_expr := @bt01_expr, 0),\n    \n        \/* bushy tree 02  *\/\n        \/* c1 op1 ((c3 op3 c4) op2 c2)  *\/\n        \/* @c_1 op_1st (( @c_3 op_3rd  @c_4) op_2nd @c_2 ) *\/\n        if(\n            @if_found = false,\n            @bt02_1 := case op_3rd\n                when '*' then @c_3 * @c_4\n                when '+' then @c_3 + @c_4\n                when '-' then @c_3 - @c_4\n                when '\/' then @c_3 \/ @c_4\n            END,\n            0\n            ) as bt02_1,\n        \n        if(\n            @if_found = false,\n            @bt02_2 := case op_2nd\n                when '*' then @bt02_1 * @c_2\n                when '+' then @bt02_1 + @c_2\n                when '-' then @bt02_1 - @c_2\n                when '\/' then @bt02_1 \/ @c_2\n            END,\n            0\n            ) as bt02_2,\n        \n        if(\n            @if_found = false,\n            @bt02_3 := case op_1st\n                \/* '+' &amp; '*' there is always a equel tree   *\/\n                when '*' then @c_1 * @bt02_2\n                when '+' then @c_1 + @bt02_2\n                when '-' then @c_1 - @bt02_2\n                when '\/' then @c_1 \/ @bt02_2\n            END,\n            0\n            ) as bt02_3,\n       \n        @bt02_expr := concat( @c_1, op_1st, \"((\", @c_3, op_3rd,  @c_4,\")\", op_2nd, @c_2, \")\"),\n        if(@bt02_3 between 24-0.0001 and 24+0.0001 , @if_found := true , 0),\n        if(@bt02_3 between 24-0.0001 and 24+0.0001 , @r_expr := @bt02_expr, 0),\n      \n      if(@if_found , @r_expr , \"false\") as result_expr,\n      \n      @if_found as if_found\n      \n      from \n            (select t.id,t.c1,t.c2,t.c3,t.c4) as \n--          (select 9 as id,9 as c1,3 as c2,1 as c3,10 as c4, @if_found := false) as \n          t_each_row , \n          \n  \n          (\n          WITH RECURSIVE \n          seq (n) as (\n          SELECT 1\n          UNION ALL\n          SELECT n + 1 FROM seq WHERE n &lt;= 3\n          )\n          select \n              seq_1.n as seq_num_1,\n              seq_2.n as seq_num_2,\n              seq_3.n as seq_num_3,\n              seq_4.n as seq_num_4\n          from \n              seq as seq_1,\n              seq as seq_2,\n              seq as seq_3,\n              seq as seq_4\n          WHERE pow(2,seq_1.n-1)+pow(2,seq_2.n-1)+pow(2,seq_3.n-1)+pow(2,seq_4.n-1) = 15\n          ) full_order\n          ,\n          (\n          WITH\n          op_list (op) as (\n          SELECT '*'\n          UNION\n          SELECT '+'\n          UNION\n          SELECT '-'\n          UNION\n          SELECT '\/'\n          )\n          SELECT op_1.op as op_1st,op_2.op as op_2nd,op_3.op as op_3rd FROM op_list as op_1,op_list as op_2,op_list as op_3\n          ) full_op\n      ) mid_result \n       WHERE\n           result_expr != \"false\"\n       LIMIT 1\n   ) mid_result_01 \n from cards as t<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9644\u5f551\uff1a\u5173\u4e8eNP-complete\u95ee\u9898<\/h3>\n\n\n\n<p>\u867d\u7136\u6ca1\u6709\u4eba\u6709\u4e25\u683c\u7684\u8bc1\u660e\uff0c\u4e0d\u8fc7\u611f\u89c9\u4e0a\uff0c24\u70b9\u95ee\u9898\u5f88\u53ef\u80fd\u662f\u4e00\u4e2aNP-\u5b8c\u5168\u95ee\u9898\u3002\u521d\u6b65\u7684\u611f\u89c9\u662f\uff0c\u4e0e<a href=\"https:\/\/en.wikipedia.org\/wiki\/Subset_sum_problem\">\u5b50\u96c6\u6c42\u548c\u95ee\u9898<\/a>\uff08<a href=\"https:\/\/en.wikipedia.org\/wiki\/Subset_sum_problem\">subset sum problem<\/a>)\u5f88\u50cf\u3002\u4ece\u89e3\u6cd5\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u7684\u201c\u52a8\u6001\u89c4\u5212\u201d\u7684\u601d\u8def\u53bb\u6c42\u89e3\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u7b80\u8ff0\u4e00\u4e0b\u4ec0\u4e48\u662fP\u95ee\u9898\uff0c\u4ec0\u4e48NP\u95ee\u9898\uff0c\u4ec0\u4e48\u662fNP-\u5b8c\u5168\u95ee\u9898\u3002\u8fd9\u662f\u4e00\u4e2a\u5728\u8ba1\u7b97\u590d\u6742\u5ea6\u5206\u6790\u9886\u57df\u7684\u95ee\u9898\uff0cP\u95ee\u9898\uff0c\u662f\u6307\u53ef\u4ee5\u5728\u591a\u9879\u5f0f\u65f6\u95f4\u5185\u6c42\u89e3\u7684\u95ee\u9898\uff1bNP\u95ee\u9898\u662f\u6307\uff0c\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\uff08\u4efb\u610f\u89e3\/\u4e5f\u53ef\u4ee5\u662f\u9519\u8bef\u89e3\uff09\u7ed9\u51fa\u540e\uff0c\u53ef\u4ee5\u5728\u591a\u9879\u5f0f\u65f6\u95f4\u5185\u9a8c\u8bc1\uff0c\u89e3\u7684\u6b63\u786e\u6027\u3002<\/p>\n\n\n\n<p>\u201cNP-\u5b8c\u5168\u95ee\u9898\u201d\uff08NP-complete problem\uff09\uff0c\u662f\u6240\u6709NP\u95ee\u9898\u4e2d\uff0c\u975e\u5e38\u96be\u7684\u4e00\u7c7b\uff0c\u5b83\u6307\u7684\u662f\uff0c\u4ee5\u5176\u4ed6\u6240\u6709\u7684NP\u95ee\u9898\u90fd\u53ef\u4ee5\u518d\u591a\u9879\u5f0f\u65f6\u95f4\u5185\u8f6c\u5316\/\u89c4\u7ea6\u4e3a\u6b64\u7c7b\u95ee\u9898\u3002\u8457\u540d\u7684NP-\u5b8c\u5168\u95ee\u9898\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u201c\u6570\u72ec\u95ee\u9898\u201d<\/li>\n\n\n\n<li>\u201c\u9b54\u65b9\u201d<\/li>\n\n\n\n<li>\u201c\u516b\u7687\u540e\u95ee\u9898\u201d<\/li>\n\n\n\n<li>\u5b50\u96c6\u6c42\u89e3\u95ee\u9898<\/li>\n\n\n\n<li>\u4e2d\u56fd\u90ae\u9012\u5458\u95ee\u9898<\/li>\n\n\n\n<li><a href=\"https:\/\/zh.wikipedia.org\/wiki\/%E6%97%85%E8%A1%8C%E6%8E%A8%E9%94%80%E5%91%98%E9%97%AE%E9%A2%98\">\u65c5\u884c\u63a8\u9500\u5458\u95ee\u9898\uff08Travelling salesman problem\uff09<\/a> \uff08\u672c\u8eab\u662f\u4e00\u4e2aNP\u95ee\u9898\uff0c\u7ed9\u5b9a\u56fe\u3001\u957f\u5ea6\uff0c\u95ee\u662f\u5426\u5b58\u5728\u66f4\u77ed\u8def\u5f84\u7684\u95ee\u9898\uff0c\u5c31\u662f\u4e00\u4e2aNP-C\u95ee\u9898\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u6b64\u5916\uff0c\u524d\u9762\u63d0\u5230\u7684<a href=\"https:\/\/en.wikipedia.org\/wiki\/Subset_sum_problem\">\u5b50\u96c6\u6c42\u548c\u95ee\u9898<\/a>\uff0c\u8be5\u95ee\u9898\uff08\u6cdb\u5316\uff09\u662f\u4e00\u4e2aNP\u95ee\u9898\uff0c\u4e00\u4e9b\u53d8\u79cd\u5219\u662fNP\u5b8c\u5168\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u53d8\u79cd\u662f\u8fd9\u6837\u7684\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5305\u542b\u82e5\u5e72\u6574\u6570\u7684\u96c6\u5408\uff0c\u95ee\uff0c\u662f\u5426\u5b58\u5728\u67d0\u4e2a\u5b50\u96c6\uff0c\u5176\u548c\u4e3a\u96f6\u3002<\/p>\n\n\n\n<p>24\u70b9\u95ee\u9898\uff0c\u4e0e\u8fd9\u4e2a\u95ee\u9898\u6709\u4e00\u4e9b\u201c\u50cf\u201d\uff0c24\u70b9\u95ee\u9898\uff0c\u662f\uff0c\u6709\u4e00\u4e2a\u96c6\u5408\u6709\u56db\u4e2a\u6570\u5b57\u548c\u56db\u4e2a\u8fd0\u7b97\uff0c\u95ee\uff0c\u662f\u5426\u5b58\u5728\u4e00\u79cd\u7ec4\u5408\u8ba9\u5176\u6570\u5b57\u548c\u8fd0\u7b97\u7b26\u6070\u597d\u7b97\u5f9724\u3002\u4e0d\u8fc7\uff0c\u8fd9\u4e2a\u95ee\u9898\u662f\u5426\u4e3aNP-C\u95ee\u9898\uff0c\u7b14\u8005\u5e76\u4e0d\u80fd\u786e\u5b9a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\uff0c\u516c\u53f8\u7ec4\u7ec7\u4e86\u4e00\u4e2a24\u70b9SQL\u7f16\u7a0b\u7684\u6bd4\u8d5b\uff0c\u7b14\u8005\u662f\u4e3b\u529e\u65b9\uff0c\u4e5f\u662f\u8bc4\u59d4\u3002\u65e2\u7136\u662f\u505a\u8bc4\u59d4\uff0c\u81ea\u5df1\u4e5f\u5148\u6311\u6218\u4e86\u4e00\u4e0b\uff0c\u56e0\u4e3a\u5bf9MySQL\u66f4\u4e3a\u719f\u6089\uff0c\u6545\u9009\u62e9\u4e86MySQL\u4f5c\u4e3a\u7f16\u7a0bSQL&#8230;<\/p>\n","protected":false},"author":1,"featured_media":22002,"comment_status":"open","ping_status":"closed","sticky":false,"template":"wp-custom-template-a-1440-px-width-template","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10639","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-simplelife"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/10639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=10639"}],"version-history":[{"count":67,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/10639\/revisions"}],"predecessor-version":[{"id":22005,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/10639\/revisions\/22005"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media\/22002"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=10639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=10639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=10639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}