1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/*
* Copyright 2010 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*
* The Computer Language Benchmarks Game
* http://shootout.alioth.debian.org/
*
* Contributed by Eckehard Berns
* Based on code by Heiner Marxen
* and the ATS version by Hongwei Xi
*
* Modified for emscripten by azakai
*/
#include <stdlib.h>
#include <stdio.h>
struct worker_args {
int i, n;
struct worker_args *next;
};
int
fannkuch_worker(void *_arg)
{
struct worker_args *args = (worker_args*)_arg;
int *perm1, *count, *perm;
int maxflips, flips, i, n, r, j, k, tmp;
maxflips = 0;
n = args->n;
perm1 = (int*)malloc(n * sizeof(int));
perm = (int*)malloc(n * sizeof(int));
count = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
perm1[i] = i;
perm1[args->i] = n - 1;
perm1[n - 1] = args->i;
r = n;
for (;;) {
for (; r > 1; r--)
count[r - 1] = r;
if (perm1[0] != 0 && perm1[n - 1] != n - 1) {
for (i = 0; i < n; i++)
perm[i] = perm1[i];
flips = 0;
k = perm[0];
do {
for (i = 1, j = k - 1; i < j; i++, j--) {
tmp = perm[i];
perm[i] = perm[j];
perm[j] = tmp;
}
flips++;
tmp = perm[k];
perm[k] = k;
k = tmp;
} while (k);
if (maxflips < flips)
maxflips = flips;
}
for (;;) {
if (r >= n - 1) {
free(perm1);
free(perm);
free(count);
return maxflips;
}
{
int p0 = perm1[0];
for (i = 0; i < r; i++)
perm1[i] = perm1[i + 1];
perm1[i] = p0;
}
if (--count[r] > 0)
break;
r++;
}
}
}
static int
fannkuch(int n)
{
struct worker_args *args, *targs;
int showmax = 30;
int *perm1, *count, i, r, maxflips, flips;
args = NULL;
for (i = 0; i < n - 1; i++) {
targs = (worker_args*)malloc(sizeof(struct worker_args));
targs->i = i;
targs->n = n;
targs->next = args;
args = targs;
}
perm1 = (int*)malloc(n * sizeof(int));
count = (int*)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
perm1[i] = i;
r = n;
for (;;) {
if (showmax) {
for (i = 0; i < n; i++)
printf("%d", perm1[i] + 1);
printf("\n");
showmax--;
} else
goto cleanup;
for (; r > 1; r--)
count[r - 1] = r;
for (;;) {
if (r == n)
goto cleanup;
{
int p0 = perm1[0];
for (i = 0; i < r; i++)
perm1[i] = perm1[i + 1];
perm1[i] = p0;
}
if (--count[r] > 0)
break;
r++;
}
}
cleanup:
free(perm1);
free(count);
maxflips = 0;
while (args != NULL) {
flips = (int)fannkuch_worker(args);
if (maxflips < flips)
maxflips = flips;
targs = args;
args = args->next;
free(targs);
}
return maxflips;
}
int main(int argc, char **argv) {
int n = argc > 1 ? atoi(argv[1]) : 0;
if (n < 1) {
printf("Wrong argument.\n");
return 1;
}
printf("Pfannkuchen(%d) = %d.\n", n, fannkuch(n));
return 0;
}
|