123456789怎样运算等于1? - abccsss 的回答
假定每个数字只能出现一次。
<code class="language-text">Det/@N@Range@9~Permutations~{9}~ArrayReshape~{9!,3,3}//Max
</code>
<code class="language-matlab"><span class="n">max_det</span> <span class="p">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="n">init_perm</span> <span class="p">=</span> <span class="nb">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="mi">9</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]);</span>
<span class="n">all_perms</span> <span class="p">=</span> <span class="nb">perms</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="mi">9</span><span class="p">);</span>
<span class="k">for</span> <span class="nb">i</span> <span class="p">=</span> <span class="mi">1</span><span class="p">:</span><span class="nb">size</span><span class="p">(</span><span class="n">all_perms</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">matrix</span> <span class="p">=</span> <span class="n">all_perms</span><span class="p">(</span><span class="nb">i</span><span class="p">,</span> <span class="p">:);</span>
<span class="n">matrix</span> <span class="p">=</span> <span class="nb">reshape</span><span class="p">(</span><span class="n">matrix</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]);</span>
<span class="n">det_value</span> <span class="p">=</span> <span class="n">det</span><span class="p">(</span><span class="n">matrix</span><span class="p">);</span>
<span class="k">if</span> <span class="n">det_value</span> <span class="o">></span> <span class="n">max_det</span>
<span class="n">max_det</span> <span class="p">=</span> <span class="n">det_value</span><span class="p">;</span>
<span class="n">init_perm</span> <span class="p">=</span> <span class="n">matrix</span><span class="p">;</span>
<span class="k">end</span>
<span class="k">end</span>
</code>
list = Permutations[Range[9], {9}];<code class="language-text">import itertools
import time
def max_matrix():
begin = time.time()
elements = [1, 2, 3, 4, 5, 6, 7, 8, 9]
maxdet = 0
maxmat = []
for i in itertools.permutations(elements, 9):
det = i[0] * i[4] * i[8] + i[1] * i[5] * i[6] + i[2] * i[3] * i[7] - i[2] * i[4] * i[6] - i[1] * i[3] * i[8] - i[0] * i[5] * i[7]
if(det > maxdet):
maxdet = det
maxmat = []
for j in range(0, 9):
maxmat.append(i[j])
print "|" + str(maxmat[0]) + " " + str(maxmat[1]) + " " + str(maxmat[2]) + "|"
print "|" + str(maxmat[3]) + " " + str(maxmat[4]) + " " + str(maxmat[5]) + "| = " + str(maxdet)
print "|" + str(maxmat[6]) + " " + str(maxmat[7]) + " " + str(maxmat[8]) + "|"
end = time.time()
print str(end - begin) + 's used.'
if __name__ == '__main__':
max_matrix()
</code>
题目应该改成1 2 3 ...n^2组成n阶行列式的最大值。并求最优解的时间复杂度才有意思。
C++:<code class="language-cpp"><span class="cp">#include <cstdio></cstdio></span>
<span class="cp">#include <algorithm></algorithm></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">ans</span><span class="p">,</span> <span class="n">a</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">};</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="k">do</span>
<span class="n">ans</span> <span class="o">=</span> <span class="n">max</span><span class="p">(</span><span class="n">ans</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">*</span> <span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">]</span> <span class="o">-</span> <span class="n">a</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">*</span> <span class="n">a</span><span class="p">[</span><span class="mi">7</span><span class="p">])</span> <span class="o">+</span>
<span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">5</span><span class="p">]</span> <span class="o">*</span> <span class="n">a</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">-</span> <span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">*</span> <span class="n">a</span><span class="p">[</span><span class="mi">8</span><span class="p">])</span> <span class="o">+</span>
<span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">*</span> <span class="n">a</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="o">-</span> <span class="n">a</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">*</span> <span class="n">a</span><span class="p">[</span><span class="mi">6</span><span class="p">]));</span>
<span class="k">while</span> <span class="p">(</span><span class="n">next_permutation</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">a</span> <span class="o">+</span> <span class="mi">9</span><span class="p">));</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">ans</span><span class="p">);</span>
<span class="p">}</span>
</code>
把yellow的答案重排一下可得<code class="language-matlab"><span class="n">p</span><span class="p">=</span><span class="nb">perms</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="mi">9</span><span class="p">);</span>
<span class="p">[</span><span class="n">n</span><span class="p">,</span><span class="o">~</span><span class="p">]=</span><span class="nb">size</span><span class="p">(</span><span class="n">p</span><span class="p">);</span>
<span class="n">z</span><span class="p">=</span><span class="nb">zeros</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="k">for</span> <span class="nb">i</span><span class="p">=</span><span class="mi">1</span><span class="p">:</span><span class="n">n</span>
<span class="n">z</span><span class="p">(</span><span class="nb">i</span><span class="p">)=</span><span class="n">det</span><span class="p">(</span><span class="nb">reshape</span><span class="p">(</span><span class="n">p</span><span class="p">(</span><span class="nb">i</span><span class="p">,:),</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">));</span>
<span class="k">end</span>
<span class="n">max</span><span class="p">(</span><span class="n">z</span><span class="p">)</span>
<span class="n">id</span><span class="p">=</span><span class="nb">find</span><span class="p">(</span><span class="n">z</span><span class="o">==</span><span class="n">max</span><span class="p">(</span><span class="n">z</span><span class="p">));</span>
<span class="k">for</span> <span class="nb">i</span><span class="p">=</span><span class="mi">1</span><span class="p">:</span><span class="nb">length</span><span class="p">(</span><span class="n">id</span><span class="p">)</span>
<span class="nb">disp</span><span class="p">(</span><span class="nb">reshape</span><span class="p">(</span><span class="n">p</span><span class="p">(</span><span class="n">id</span><span class="p">(</span><span class="nb">i</span><span class="p">),:),</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">));</span>
<span class="k">end</span>
</code>
对于三阶的穷举,可以不用det函数会比较简单:<code class="language-matlab"><span class="n">p</span> <span class="p">=</span> <span class="nb">reshape</span><span class="p">(</span><span class="nb">perms</span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="mi">9</span><span class="p">),</span><span class="s">''</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">);</span>
<span class="n">M</span> <span class="p">=</span> <span class="n">max</span><span class="p">(</span><span class="n">sum</span><span class="p">(</span><span class="n">prod</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="mi">2</span><span class="p">),</span><span class="mi">3</span><span class="p">)</span><span class="o">-</span><span class="n">sum</span><span class="p">(</span><span class="n">prod</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span><span class="mi">2</span><span class="p">));</span>
</code>
话题的语言还少个Mathematica,就我来吧<code class="language-text">Det[Partition[#, 3]] & /@ Permutations[Range[9]] // Max
412
</code>