多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 十、Python编程计算24点(之二) ----From a high school student's view to learnPython 关键字: python 高中生学编程 Python编程入门 计算24点表达式计算 组合问题 三、程序实现 说完了该列举的问题,接下来就是程序的书写部分了,现在我们要回忆之前学的一系列知识,包括函数,列表,循环等等。首先我们来解决组合的书写问题,因为有四个数,我们便可以把这四个数归到同一个列表中,然后再通过调用来运用,但是在这里我们需要创造两个列表,一个是函数里的列表,另一个是函数外引入的参数,因为是要写组合,所以我们就应该将引入的参数分别的赋值给我们在函数内新建的列表,然后再在之后的程序中使用已被赋值的列表。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p></td><td valign="top"><p><span> <wbr>  <wbr>a=[</wbr></wbr></span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>]</span></p><p><span> <wbr>  <wbr/></wbr></span><span>for</span> <span>i1</span> <span>in</span><span>range(</span><span>3</span><span>):</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>0</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>1</span><span>];a[</span><span>2</span><span>]=c[</span><span>2</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>1</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>2</span><span>];a[</span><span>2</span><span>]=c[</span><span>1</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>2</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>3</span><span>];a[</span><span>2</span><span>]=c[</span><span>1</span><span>];a[</span><span>3</span><span>]=c[</span><span>2</span><span>]</span></p></td></tr></tbody></table> a就是我们新建的列表,其实列表里的元素随便是什么都可以,只要有四个,因为会被替换掉,接下来就需要用一个循环来解决组合问题。由于有三种情况,所以我们可以用一个三个条件的循环,分别是(ab)(cd) ,(ac) (bd) ,(ad) (bc) 然后再对range函数进行for语句循环操作。这里要注意的一点是:range中是从0开始计算,而且结尾应是前一个数,所以是以3结尾。这样我们就成功的写出了循环的组合语句,我们就不用再在之后的程序中浪费时间重复操作了。 四选三的组合语句也差不多,如下 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p></td><td valign="top"><p><span> <wbr>  <wbr>a=[</wbr></wbr></span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>]</span></p><p><span> <wbr>  <wbr>sign=[</wbr></wbr></span><span><i>'+'</i></span><span>,</span><span><i>'-'</i></span><span>]</span></p><p><span> <wbr>  <wbr/></wbr></span><span>for</span> <span>i1</span> <span>in</span><span>range(</span><span>4</span><span>):</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>0</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>1</span><span>];a[</span><span>2</span><span>]=c[</span><span>2</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>1</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>1</span><span>];a[</span><span>2</span><span>]=c[</span><span>3</span><span>];a[</span><span>3</span><span>]=c[</span><span>2</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>2</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>2</span><span>];a[</span><span>2</span><span>]=c[</span><span>3</span><span>];a[</span><span>3</span><span>]=c[</span><span>1</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>3</span><span>:a[</span><span>0</span><span>]=c[</span><span>1</span><span>];a[</span><span>1</span><span>]=c[</span><span>2</span><span>];a[</span><span>2</span><span>]=c[</span><span>3</span><span>];a[</span><span>3</span><span>]=c[</span><span>0</span><span>]</span></p></td></tr></tbody></table> 接下来我们就要进行枚举各种情况了,但是在枚举前,我们应该再思考一下到底有什么地方可以在进行简化。仔细观察前面列的所有情况,我们可以发现含有+-这两个符号的式子很多,有些式子甚至含有两对到三对,但是如果我们一一去列举所有的情况,未免太复杂:+++,++-,+--,+-+,-++,-+-,--+,---一共八种,也就是说我们要为了之一种简单的情况写八遍差不多的代码,那我们为什么不做一个甚至多重的for循环呢? <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p></td><td valign="top"><p><span> <wbr>  <wbr>sign=[</wbr></wbr></span><span><i>'+'</i></span><span>,</span><span><i>'-'</i></span><span>]</span></p><p><span> <wbr>  <wbr> for</wbr></wbr></span><span>i2</span> <span>in</span> <span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr/></wbr></wbr></wbr></wbr></wbr></span><span>for</span> <span>i3</span> <span>in</span><span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> <span>for</span><span>i4</span> <span>in</span> <span>sign:</span></p></td></tr></tbody></table> 首先建立一个列表(line1)然后再做一个三重循环,当然这里的循环次数和式子中+-的个数要保持一致。但是为什么*/不用for循环来做呢,因为我们发现其实(c*/d) <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p><p><span>16</span></p><p><span>17</span></p></td><td valign="top"><p><span> <wbr>  <wbr>num4list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>a=[</wbr></wbr></span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>]</span></p><p><span> <wbr>  <wbr/></wbr></span><span>for</span> <span>i1</span> <span>in</span><span>range(</span><span>3</span><span>):</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>0</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>1</span><span>];a[</span><span>2</span><span>]=c[</span><span>2</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>1</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>2</span><span>];a[</span><span>2</span><span>]=c[</span><span>1</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>2</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>3</span><span>];a[</span><span>2</span><span>]=c[</span><span>1</span><span>];a[</span><span>3</span><span>]=c[</span><span>2</span><span>]</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'*'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'*'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'*'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(</wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'*'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>')'</i></span><span>)</span></p></td></tr></tbody></table> 看起来很复杂,其实找到规律之后就十分容易了,由于/的符号有顺序一说(a/b和b/a的不同)所以我们需要调换顺序,然后依照规律,我们就可以列出一共10种不同的情况。要注意的是,如果不是str()函数,则需要用双引号来表示字符串的意义。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p></td><td valign="top"><p><span> <wbr>  <wbr>  <wbr>num4list.append(str(a[</wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr> <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>]))</span></p><p><span> <wbr/></span><span> <wbr> <wbr>  <wbr>num4list.append(str(a[</wbr></wbr></wbr></span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>]))</span></p></td></tr></tbody></table> 这种情况就是a*/b+-c*/d,由于ab和cd之间都有除号,所以这两个字母都需要调换顺序,而且因为中间的符号有减号,所以我们还需要将前两个数字和后两个数字调换顺序来满足所有的情况。而由于只里面只有一个+-所以并不需要用循环来简化,只需枚举即可。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p><p><span>16</span></p></td><td valign="top"><p><span> <wbr>  <wbr>num3list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>sign=[</wbr></wbr></span><span><i>'+'</i></span><span>,</span><span><i>'-'</i></span><span>]</span></p><p><span> <wbr>  <wbr>a=[</wbr></wbr></span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>]</span></p><p><span> <wbr>  <wbr/></wbr></span><span>for</span> <span>i1</span> <span>in</span><span>range(</span><span>3</span><span>):</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>0</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>1</span><span>];a[</span><span>2</span><span>]=c[</span><span>2</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>1</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>2</span><span>];a[</span><span>2</span><span>]=c[</span><span>1</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>2</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>3</span><span>];a[</span><span>2</span><span>]=c[</span><span>1</span><span>];a[</span><span>3</span><span>]=c[</span><span>2</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>for</span> <span>i2</span><span>in</span> <span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr/></wbr></wbr></wbr></wbr></wbr></span><span>for</span> <span>i3</span> <span>in</span><span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> <span>for</span><span>i4</span> <span>in</span> <span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> <span>if</span><span>i2==</span><span><i>'+'</i></span><span>:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>i2=</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span><i>''</i></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num3list.append(i2+</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+i3+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'*'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+i4+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num3list.append(i2+</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+i3+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+i4+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>)</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num3list.append(i2+</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+str(a[</span><span>2</span><span>])+i3+str(a[</span><span>3</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+</span><span><i>'('</i></span><span>+str(a[</span><span>0</span><span>])+i4+str(a[</span><span>1</span><span>])+</span><span><i>')'</i></span><span>)</span></p></td></tr></tbody></table> 这是(a+-b)*/(c+-d)的情况,要注意的是,在这几行代码中我们用到了一个三重循环,而+-只有两种,原因是我们在括号的最前面又加了一个符号,仔细观察,我们可以发现这个符号是对第一个括号内的正负进行的判定,因为在第一个括号内会出现a与b调换的情况,而且我们发现第一个括号内会出现答案是负数的情况,但是不要忘了,在第二个括号内的情况和第一个括号内的情况是完全相同的,所以第二个括号内也会出现负数的情况,当第一和第二个括号内的结果均为负数时,就又变成了正数(中间是*/),所以我们并不用在每个括号前加一个符号,取而代之的是在整个式子之前加正负号就可以了。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p><p><span>16</span></p><p><span>17</span></p></td><td valign="top"><p><span> <wbr>  <wbr>num4list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>a=[99,99,99,99]</wbr></wbr></span></p><p><span> <wbr>  <wbr> for i1 inrange(3):</wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==0:a[0]=c[0];a[1]=c[1];a[2]=c[2];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==1:a[0]=c[0];a[1]=c[2];a[2]=c[1];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==2:a[0]=c[0];a[1]=c[3];a[2]=c[1];a[3]=c[2]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr> <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[0])+'*'+str(a[1])+')'+'/'+'('+str(a[2])+'*'+str(a[3])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[2])+'*'+str(a[3])+')'+'/'+'('+str(a[0])+'*'+str(a[1])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[2])+'*'+str(a[3])+')'+'/'+'('+str(a[0])+'/'+str(a[1])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[2])+'*'+str(a[3])+')'+'/'+'('+str(a[1])+'/'+str(a[0])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[0])+'*'+str(a[1])+')'+'/'+'('+str(a[2])+'/'+str(a[3])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[0])+'*'+str(a[1])+')'+'/'+'('+str(a[3])+'/'+str(a[2])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[0])+'*'+str(a[1])+')'+'*'+'('+str(a[2])+'/'+str(a[3])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[0])+'*'+str(a[1])+')'+'*'+'('+str(a[3])+'/'+str(a[2])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[2])+'*'+str(a[3])+')'+'*'+'('+str(a[0])+'/'+str(a[1])+')')</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append('('+str(a[2])+'*'+str(a[3])+')'+'*'+'('+str(a[1])+'/'+str(a[0])+')')</wbr></wbr></wbr></wbr></span></p></td></tr></tbody></table> 这是(a*/b)*/(c*/d)的情况,大家如果观察可以发现第一个括号内并没有/的情况,这是因为如果第一个括号内的符号是/,两个括号中间的符号必须是*,否则答案不可能是24,但是如果中间的符号是*,就没必要考虑两个括号的摆放顺序的问题了,所以我索性让第一个括号的符号均为*,看起来也比较整齐。其它的步骤还是和前几个差不多,调换/两端元素的位置,找到规律之后十分简单。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p></td><td valign="top"><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'+'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>0</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>num4list.append(str(a[</wbr></wbr></wbr></wbr></span><span>2</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'-'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>]))</span></p></td></tr></tbody></table> 这种情况就是a*/b+-c*/d,由于也是两个数字一组的情况,之前的组合代码和上一种情况相同,所以我就索性将两种情况合并在一起,节省了很多时间,但是要注意的是这种情况包含两个*/和一个+-,所以情况比较多,但是/-/的情况显然不可能所以排除,大家要注意不要遗漏或者重复。 接下来就是三个一组的情况: <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p><p><br/></p></td><td valign="top"><p><span> <wbr>  <wbr>num5list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>a=[</wbr></wbr></span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>]</span></p><p><span> <wbr>  <wbr>sign=[</wbr></wbr></span><span><i>'+'</i></span><span>,</span><span><i>'-'</i></span><span>]</span></p><p><span> <wbr>  <wbr/></wbr></span><span>for</span> <span>i1</span> <span>in</span><span>range(</span><span>4</span><span>):</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>0</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>1</span><span>];a[</span><span>2</span><span>]=c[</span><span>2</span><span>];a[</span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>1</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>1</span><span>];a[</span><span>2</span><span>]=c[</span><span>3</span><span>];a[</span><span>3</span><span>]=c[</span><span>2</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>2</span><span>:a[</span><span>0</span><span>]=c[</span><span>0</span><span>];a[</span><span>1</span><span>]=c[</span><span>2</span><span>];a[</span><span>2</span><span>]=c[</span><span>3</span><span>];a[</span><span>3</span><span>]=c[</span><span>1</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>if</span><span>i1==</span><span>3</span><span>:a[</span><span>0</span><span>]=c[</span><span>1</span><span>];a[</span><span>1</span><span>]=c[</span><span>2</span><span>];a[</span><span>2</span><span>]=c[</span><span>3</span><span>];a[</span><span>3</span><span>]=c[</span><span>0</span><span>]</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></span> <span>for</span> <span>i2</span><span>in</span> <span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr/></wbr></wbr></wbr></wbr></wbr></span><span>for</span> <span>i3</span> <span>in</span><span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> <span>for</span><span>i4</span> <span>in</span> <span>sign:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr/></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span> <span>if</span><span>i2==</span><span><i>'+'</i></span><span>:</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>i2=</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span><i>''</i></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num5list.append(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+i2+str(a[</span><span>0</span><span>])+i3+str(a[</span><span>1</span><span>])+i4+str(a[</span><span>2</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num5list.append(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><span><i>'('</i></span><span>+i2+str(a[</span><span>0</span><span>])+i3+str(a[</span><span>1</span><span>])+i4+str(a[</span><span>2</span><span>])+</span><span><i>')'</i></span><span>+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p></td></tr></tbody></table> (a+-b+-c)*/d由于是三个一组的情况,之前在组合问题中说过有四种情况,而且由于前两个符号都是+-,而我又在第一个数字前加了一个符号来避免调换顺序,我们可以用一个三重循环来解决重复书写的问题,和之前介绍过的技巧一样。同样大家也要注意第一个符号是+的情况是无意义的,所以要把它去掉(line12-line13)。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p><p><span>16</span></p><p><span>17</span></p><p><span>18</span></p><p><span>19</span></p><p><span>20</span></p><p><span>21</span></p><p><span>22</span></p><p><span>23</span></p><p><span>24</span></p><p><span>25</span></p><p><span>26</span></p><p><span>27</span></p><p><span>28</span></p><p><span>29</span></p><p><span>30</span></p><p><span>31</span></p><p><span>32</span></p><p><span>33</span></p><p><span>34</span></p><p><span>35</span></p><p><span>36</span></p><p><span>37</span></p><p><span>38</span></p><p><span>39</span></p><p><span>40</span></p><p><span>41</span></p><p><span>42</span></p><p><span>43</span></p><p><span>44</span></p></td><td valign="top"><p><span> <wbr>  <wbr>num6list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>a=[99,99,99,99]</wbr></wbr></span></p><p><span> <wbr>  <wbr>sign=['+','-']</wbr></wbr></span></p><p><span> <wbr>  <wbr> for i1 inrange(4):</wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==0: a[0]=c[0]; a[1]=c[1]; a[2]=c[2];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==1: a[0]=c[0]; a[1]=c[1]; a[2]=c[3];a[3]=c[2]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==2: a[0]=c[0]; a[1]=c[2]; a[2]=c[3];a[3]=c[1]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==3: a[0]=c[1]; a[1]=c[2]; a[2]=c[3];a[3]=c[0]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> for i2 in sign:</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>num6list.append(str(a[0])+'*'+str(a[1])+'*'+str(a[2])+i2+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>num6list.append(str(a[0])+'*'+str(a[1])+'/'+str(a[2])+i2+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>num6list.append(str(a[0])+'*'+str(a[2])+'/'+str(a[1])+i2+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>num6list.append(str(a[2])+'*'+str(a[1])+'/'+str(a[0])+i2+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> for i3 insign:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr> if i2=='-' and i3=='-':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>continue</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr> if i3=='+':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr>i3=''</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[0])+i2+str(a[1])+'*'+str(a[2])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[0])+i2+str(a[1])+'/'+str(a[2])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[0])+i2+str(a[2])+'/'+str(a[1])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[0])+i2+str(a[1])+'*'+str(a[2])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[0])+i2+str(a[1])+'/'+str(a[2])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[0])+i2+str(a[2])+'/'+str(a[1])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[1])+i2+str(a[0])+'*'+str(a[2])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[1])+i2+str(a[0])+'/'+str(a[2])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[1])+i2+str(a[2])+'/'+str(a[0])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[1])+i2+str(a[0])+'*'+str(a[2])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[1])+i2+str(a[0])+'/'+str(a[2])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[1])+i2+str(a[2])+'/'+str(a[0])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[2])+i2+str(a[1])+'*'+str(a[0])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[2])+i2+str(a[1])+'/'+str(a[0])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[2])+i2+str(a[0])+'/'+str(a[1])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[2])+i2+str(a[1])+'*'+str(a[0])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[2])+i2+str(a[1])+'/'+str(a[0])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append('('+i3+str(a[2])+i2+str(a[0])+'/'+str(a[1])+')'+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[0])+i2+str(a[1])+'*'+str(a[2])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[0])+i2+str(a[2])+'/'+str(a[1])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[0])+i2+str(a[1])+'/'+str(a[2])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[1])+i2+str(a[0])+'*'+str(a[2])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[1])+i2+str(a[0])+'/'+str(a[2])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[1])+i2+str(a[2])+'/'+str(a[0])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[2])+i2+str(a[1])+'*'+str(a[0])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[2])+i2+str(a[1])+'/'+str(a[0])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>num6list.append(i3+str(a[3])+'/'+'('+str(a[2])+i2+str(a[0])+'/'+str(a[1])+')')</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p></td></tr></tbody></table> 接下来这个是a*/b*/c+-d和(a+-b*/c)*/d的两种情况的合并,由于这两种都是三个一组的情况,合并可以少些很多重复的代码,但是要注意的是,这两种情况并不是在最后合并,它们各自的前提条件的代码有所区别。因为第一个式子中只有一个+-,而第二个式子中有两个(式子最前要加一个符号),所以所进行的循环操作的次数是不一样的,但是我们仍然可以把它们合并在一起,只需在第一个循环操作结束后直接输出第一个式子的结果,把它们加入到列表中,然后再进行下两个循环操作就可以了。但是对于第二个式子,我们要注意的是在括号之后的符号有/,所以就不可避免的要调换位置,但是再调换位置之后会出现代码不整齐的情况,影响美观,所以我们不妨把这一类单分出来放在最下面。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p></td><td valign="top"><p><span> <wbr>  <wbr>num7list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>a=[</wbr></wbr></span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>,</span><span>99</span><span>]</span></p><p><span> <wbr>  <wbr>a[</wbr></wbr></span><span>0</span><span>]=c[</span><span>0</span><span>]</span></p><p><span> <wbr>  <wbr>a[</wbr></wbr></span><span>1</span><span>]=c[</span><span>1</span><span>]</span></p><p><span> <wbr>  <wbr>a[</wbr></wbr></span><span>2</span><span>]=c[</span><span>2</span><span>]</span></p><p><span> <wbr>  <wbr>a[</wbr></wbr></span><span>3</span><span>]=c[</span><span>3</span><span>]</span></p><p><span> <wbr>  <wbr>num7list.append(str(a[</wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>3</span><span>]))</span></p><p><span> <wbr>  <wbr>num7list.append(str(a[</wbr></wbr></span><span>3</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>0</span><span>]))</span></p><p><span> <wbr>  <wbr>num7list.append(str(a[</wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>2</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>1</span><span>]))</span></p><p><span> <wbr>  <wbr>num7list.append(str(a[</wbr></wbr></span><span>0</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>1</span><span>])+</span><span><i>'*'</i></span><span>+str(a[</span><span>3</span><span>])+</span><span><i>'/'</i></span><span>+str(a[</span><span>2</span><span>]))</span></p></td></tr></tbody></table> 这种就是a*/b*/c*/d的情况,但是由于一乘两除或者三个除号的情况是不可能形成24的,所以我们这里只考虑两乘一除的情况,由于没有+-,我们就不需要组合的代码,只需将c列表中的元素赋值给a列表中的每个元素即可。 <table cellspacing="0" cellpadding="0"><tbody><tr><td valign="top"><p><span>1</span></p><p><span>2</span></p><p><span>3</span></p><p><span>4</span></p><p><span>5</span></p><p><span>6</span></p><p><span>7</span></p><p><span>8</span></p><p><span>9</span></p><p><span>10</span></p><p><span>11</span></p><p><span>12</span></p><p><span>13</span></p><p><span>14</span></p><p><span>15</span></p><p><span>16</span></p><p><span>17</span></p><p><span>18</span></p><p><span>19</span></p><p><span>20</span></p><p><span>21</span></p><p><span>22</span></p><p><span>23</span></p><p><span>24</span></p><p><span>25</span></p><p><span>26</span></p><p><span>27</span></p><p><span>28</span></p><p><span>29</span></p><p><span>30</span></p><p><span>31</span></p><p><span>32</span></p><p><span>33</span></p><p><span>34</span></p><p><span>35</span></p><p><span>36</span></p><p><span>37</span></p><p><span>38</span></p><p><span>39</span></p><p><span>40</span></p><p><span>41</span></p><p><span>42</span></p><p><span>43</span></p><p><span>44</span></p><p><span>45</span></p><p><span>46</span></p><p><span>47</span></p></td><td valign="top"><p><span> <wbr>  <wbr>num8list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>a=[99,99,99,99]</wbr></wbr></span></p><p><span> <wbr>  <wbr>sign=['+','-']</wbr></wbr></span></p><p><span> <wbr>  <wbr> for i1 inrange(6):</wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==0:a[0]=c[0];a[1]=c[1];a[2]=c[2];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==1:a[0]=c[0];a[1]=c[2];a[2]=c[1];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==2:a[0]=c[0];a[1]=c[3];a[2]=c[1];a[3]=c[2]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==3:a[0]=c[2];a[1]=c[3];a[2]=c[0];a[3]=c[1]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==4:a[0]=c[1];a[1]=c[3];a[2]=c[0];a[3]=c[2]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==5:a[0]=c[1];a[1]=c[2];a[2]=c[0];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> for i2 in sign:</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> for i3 insign:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr> if i2=='-' andi3=='-':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr> continue</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr> if i2=='+':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr> i2=''</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[2])+'+'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[3])+'+'+str(a[2]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[2])+'-'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[3])+'-'+str(a[2]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'/'+str(a[2])+'+'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'/'+str(a[3])+'+'+str(a[2]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'/'+str(a[2])+'-'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num8list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'/'+str(a[3])+'-'+str(a[2]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><br/></p><p><span> <wbr>  <wbr>num10list=[]</wbr></wbr></span></p><p><span> <wbr>  <wbr>a=[99,99,99,99]</wbr></wbr></span></p><p><span> <wbr>  <wbr>sign=['+','-']</wbr></wbr></span></p><p><span> <wbr>  <wbr> for i1 inrange(6):</wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==0:a[0]=c[0];a[1]=c[1];a[2]=c[2];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==1:a[0]=c[0];a[1]=c[2];a[2]=c[1];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==2:a[0]=c[0];a[1]=c[3];a[2]=c[1];a[3]=c[2]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==3:a[0]=c[2];a[1]=c[3];a[2]=c[0];a[3]=c[1]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==4:a[0]=c[1];a[1]=c[3];a[2]=c[0];a[3]=c[2]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> if i1==5:a[0]=c[1];a[1]=c[2];a[2]=c[0];a[3]=c[3]</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr> for i2 in sign:</wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> for i3 insign:</wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr> if i2=='-' andi3=='-':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr> continue</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr> if i2=='+':</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr> i2=''</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num10list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[2])+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num10list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'/'+str(a[2])+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num10list.append('('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[2])+'/'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num10list.append(str(a[2])+'/'+'('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[3]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><span> <wbr>  <wbr>  <wbr> <wbr>  <wbr>  <wbr> <wbr>  <wbr>num10list.append(str(a[3])+'/'+'('+i2+str(a[0])+i3+str(a[1])+')'+'*'+str(a[2]))</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></p><p><br/></p></td></tr></tbody></table> 接下来的这两种情况就是最复杂的组合情况了,(a+-b)*/c+-d和(a+-b)*/c*/d这两种情况下的组合,由于这两种情况下用六种的组合情况可以少写一些代码,并且更加简单,我们不妨把三种的组合代码改为六种。  到此为止,我们就把所有的组合的情况都列了出来,但是还有一些后续的程序来保证程序的运行和美观。第一就是我们应该把每一种情况的代码都写入到一个函数中,这样如果我们以后要用到的话,直接调用函数就能解决问题,第二就是我们还应该把所有的情况都加在一起,因为验证的时候要用到的是所有的情况,而把它们合并到一个大函数中就不用一个一个调用了。 四、如何利用之前的函数来进行计算 由于我们的程序越做越大,如果我们还把所有的函数,代码都放在一个模块中,显然是十分的复杂以及不方便,这时我们就要把代码写入不同的文件中,但是最后运行的时候,每个文件都需要被运行,那们我们怎么达成呢?这时我们就要用到import语句了,import语句的主要作用就是将多个文件引入到一个文件中,从而在运行一个文件时,其他的文件也能够被运行。 import语句的语法: import 语句的模块顺序 我们推荐所有的模块在 Python 模块的开头部分导入。而且好按照这样的顺序: Python 标准库模块  Python 第三方模块 应用程序自定义模块  然后使用一个空行分割这三类模块的导入语句。 这将确保模块使用固定的习惯导入, 有助于减 少每个模块需要的import 语句数目。 其他的提示请参考《 Python 风格指南》(Python’s Style Guide),PEP8 fromtransitionimport* fromexcercise4importbiglist  fromdisorderimportchange_order fromexcercise1importcirculate 这就是我用到的几个import语句。 我的更多文章: - [Python程序调试的一些体会](http://blog.sina.com.cn/s/blog_d6cca93e0101ewc9.html)(2013-10-06 22:57:35) - [十三、Python编程计算24点(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101eukc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片") (2013-10-02 22:15:46) - [十二、Python简单数据结构应用(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101euk8.html)(2013-10-02 22:10:41) - [十一、Python简单数据结构应用(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ep9z.html)(2013-09-23 23:31:49) - [十、Python编程解决组合问题(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101entc.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含图片") (2013-09-21 23:37:27) - [九、Python编程解决组合问题(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ent7.html)(2013-09-21 23:32:54) - [八、Python的函数编程(之二)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwj.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频") (2013-09-20 23:09:39) - [七、Python的函数编程(之一)](http://blog.sina.com.cn/s/blog_d6cca93e0101ekwg.html)![](https://box.kancloud.cn/2015-10-30_5632e1cc04fc3.gif "此博文包含视频") (2013-09-20 23:09:10) - [高中生如何学编程](http://blog.sina.com.cn/s/blog_d6cca93e0101e8fn.html)(2013-09-02 19:26:01)