本帖最后由 青青子衿 于 2019-6-3 14:23 编辑
回复 1# wujiemin1981
某单位停车场内五俩汽车并排停在一起,下班时它们要分别驶离,按规定每次都是两边上的其中一辆车先走,如果中间的某辆先走算抢队 ...
wujiemin1981 发表于 2019-6-3 09:57
对五辆车进行标记,从左往右依次标为1、2、3、4、5号车,
则可以得到出车“代序排列”(代表顺序的排列),
于是给出一个满足不抢队的序列:AA={5,1,4,3,2}
AA这个“代序排列”代表的是:
第一步,先出最右侧的5号车,剩下四辆车;
第二步,然后出最左侧的1号车,剩下三辆车;
第三步,然后出最右侧的4号车,剩下两辆车;
第四步,然后出最右侧的3号车,剩下一辆车;
第五步,最后出2号车,结束。
\begin{gather*}
1\qquad2\qquad3\qquad4\qquad5\\
\phantom{\small{5}}\downarrow\small{5}\\
1\qquad2\qquad3\qquad4\qquad{\phantom5}\\
\phantom{\small{1}}\downarrow\small{1}\\
{\phantom1}\qquad2\qquad3\qquad4\qquad{\phantom5}\\
\phantom{\small{4}}\downarrow\small{4}\\
{\phantom1}\qquad2\qquad3\qquad{\phantom4}\qquad{\phantom5}\\
\phantom{\small{3}}\downarrow\small{3}\\
{\phantom1}\qquad2\qquad{\phantom3}\qquad{\phantom4}\qquad{\phantom5}\\
\phantom{\small{2}}\downarrow\small{2}\\
\\
\end{gather*}
于是,一共有 16种 出五辆车的出车顺序满足“不抢队”的条件。
(先在5号车与1号车里二选一,再在剩下的车组里最左最右二选一,
继续在剩下的车组里最左最右二选一,以此类推)
\(\,\,2^4=16\,\,\)
也就是说,出五辆车有零辆车“抢队”,一共有 16种 情况 。
{{1, 2, 3, 4, 5}, {1, 2, 3, 5, 4}, {1, 2, 5, 3, 4}, {1, 2, 5, 4, 3},
{1, 5, 2, 3, 4}, {1, 5, 2, 4, 3}, {1, 5, 4, 2, 3}, {1, 5, 4, 3, 2},
{5, 1, 2, 3, 4}, {5, 1, 2, 4, 3}, {5, 1, 4, 2, 3}, {5, 1, 4, 3, 2},
{5, 4, 1, 2, 3}, {5, 4, 1, 3, 2}, {5, 4, 3, 1, 2}, {5, 4, 3, 2, 1}}
那么,出五辆车恰有一辆车“抢队”,一共有 ??种 情况 。
【代码一】- f[list_] := Module[
- {k}, k = 0;
- For[
- i = 1, i < Length[list], i++,
- If[
- Min[list[[i + 1 ;; Length[list]]]]
- < list[[i]] <
- Max[list[[i + 1 ;; Length[list]]]],
- k = 1;
- Break[]
- ]
- ];
- k == 0]
- Select[Permutations[Range[5]], f]
复制代码 ***** ***** ***** ***** ***** *****
【代码二】- ff[ans_, lis_] := ff[ans, lis] =
- If[Length@lis == 1,
- Flatten@{ans, lis}, {ff[Flatten@{ans, First@lis}, Rest@lis],
- ff[Flatten@{ans, Last@lis}, Most@lis]}];
- lis = Range@5;
- ff[{}, Sort@lis] // Flatten[#, Length@lis - 2] &
复制代码 |