Fannkuch如何运作?

mud*_*dge 7 algorithm

我无法理解实施Fannkuch的指示.说明:http://www.haskell.org/haskellwiki/Shootout/Fannkuch

在"计算翻转次数,这里5"之后,我迷路了.

Gar*_*ott 5

哇,是的,这不是最好的算法描述:).

我的解释是他们希望你做以下事情:

fannkuch(n) {
 int maxFlips = 0, printCount = 0;
 foreach permutation p of [1..n] {
  maxFlips = max(maxFlips, flipCount(p));
  if (printCount++ < 30) printPermutation(p);
 }
 print(maxFlips);
}

flipCount(p) {
 int count = 0;
 while (p[0] != 1) {
  reverse(p, p + p[0]);
  count++;
 }
 return count;
}