#include<stdio.h>
// 並べ替え結果処理
void kekka_shori(int *d, int len)
{
int i, j, tmp[2][5], *p[2], *swap, ans[15], *pa = &ans[0];
unsigned short b = 0x0000;
for (i = 0; i < 5; i++) {
*pa++ = tmp[0][i] = d[i];
}
p[0] = &tmp[0][0];
p[1] = &tmp[1][0];
for (j = 5; j >= 2; j--) {
for (i = 0; i < j; i++) {
b |= 1 << (p[0][i] - 1);
}
for (i = 0; i < j - 1; i++) {
p[1][i] = p[0][i] - p[0][i + 1];
if (p[1][i] < 0) {
p[1][i] = -p[1][i];
}
*pa++ = p[1][i];
}
// swap
swap = p[0];
p[0] = p[1];
p[1] = swap;
}
b |= 1 << (p[0][0] - 1);
if (b == 0x7FFF) {
pa = &ans[0];
for (j = 5; j >= 1; j--) {
printf("%*s", (5 - j
) * 2, ""); for (i = 0; i < j; i++) {
}
}
}
}
// 並べ替える
// (len個の配列の右n個を左にスライドして再帰)
void narabe(int *d, int len, int n)
{
int i, j;
int tmp;
if (n <= 10) {
kekka_shori(d, len);
return;
}
for (j = 0; j < n; j++) {
narabe(d, len, n - 1);
tmp = d[len - n];
for (i = 0; i < n - 1; i++) {
d[len - n + i] = d[len - n + i + 1];
}
d[len - n + i] = tmp;
}
}
int main()
{
int d[15];
int i;
for (i = 0; i < 15; i++) {
d[i] = i + 1;
}
narabe(d, 15, 15);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCi8vIOS4puOBueabv+OBiOe1kOaenOWHpueQhgp2b2lkIGtla2thX3Nob3JpKGludCAqZCwgaW50IGxlbikKewogICAgaW50IGksIGosIHRtcFsyXVs1XSwgKnBbMl0sICpzd2FwLCBhbnNbMTVdLCAqcGEgPSAmYW5zWzBdOwogICAgdW5zaWduZWQgc2hvcnQgYiA9IDB4MDAwMDsKCiAgICBmb3IgKGkgPSAwOyBpIDwgNTsgaSsrKSB7CiAgICAgICAgKnBhKysgPSB0bXBbMF1baV0gPSBkW2ldOwogICAgfQogICAgcFswXSA9ICZ0bXBbMF1bMF07CiAgICBwWzFdID0gJnRtcFsxXVswXTsKICAgIGZvciAoaiA9IDU7IGogPj0gMjsgai0tKSB7CiAgICAgICAgZm9yIChpID0gMDsgaSA8IGo7IGkrKykgewogICAgICAgICAgICBiIHw9IDEgPDwgKHBbMF1baV0gLSAxKTsKICAgICAgICB9CiAgICAgICAgZm9yIChpID0gMDsgaSA8IGogLSAxOyBpKyspIHsKICAgICAgICAgICAgcFsxXVtpXSA9IHBbMF1baV0gLSBwWzBdW2kgKyAxXTsKICAgICAgICAgICAgaWYgKHBbMV1baV0gPCAwKSB7CiAgICAgICAgICAgICAgICBwWzFdW2ldID0gLXBbMV1baV07CiAgICAgICAgICAgIH0KICAgICAgICAgICAgKnBhKysgPSBwWzFdW2ldOwogICAgICAgIH0KICAgICAgICAvLyBzd2FwCiAgICAgICAgc3dhcCA9IHBbMF07CiAgICAgICAgcFswXSA9IHBbMV07CiAgICAgICAgcFsxXSA9IHN3YXA7CiAgICB9CiAgICBiIHw9IDEgPDwgKHBbMF1bMF0gLSAxKTsKCiAgICBpZiAoYiA9PSAweDdGRkYpIHsKICAgICAgICBwYSA9ICZhbnNbMF07CiAgICAgICAgcHJpbnRmKCItLS1cbiIpOwogICAgICAgIGZvciAoaiA9IDU7IGogPj0gMTsgai0tKSB7CiAgICAgICAgICAgIHByaW50ZigiJSpzIiwgKDUgLSBqKSAqIDIsICIiKTsKICAgICAgICAgICAgZm9yIChpID0gMDsgaSA8IGo7IGkrKykgewogICAgICAgICAgICAgICAgcHJpbnRmKCIlMmQgICIsICpwYSsrKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBwcmludGYoIlxuIik7CiAgICAgICAgfQogICAgfQp9CgovLyDkuKbjgbnmm7/jgYjjgosKLy8gIChsZW7lgIvjga7phY3liJfjga7lj7Nu5YCL44KS5bem44Gr44K544Op44Kk44OJ44GX44Gm5YaN5biwKQp2b2lkIG5hcmFiZShpbnQgKmQsIGludCBsZW4sIGludCBuKQp7CiAgICBpbnQgaSwgajsKICAgIGludCB0bXA7CgogICAgaWYgKG4gPD0gMTApIHsKICAgICAgICBrZWtrYV9zaG9yaShkLCBsZW4pOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIGZvciAoaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICBuYXJhYmUoZCwgbGVuLCBuIC0gMSk7CiAgICAgICAgdG1wID0gZFtsZW4gLSBuXTsKICAgICAgICBmb3IgKGkgPSAwOyBpIDwgbiAtIDE7IGkrKykgewogICAgICAgICAgICBkW2xlbiAtIG4gKyBpXSA9IGRbbGVuIC0gbiArIGkgKyAxXTsKICAgICAgICB9CiAgICAgICAgZFtsZW4gLSBuICsgaV0gPSB0bXA7CiAgICB9Cn0KCmludCBtYWluKCkKewogICAgaW50IGRbMTVdOwogICAgaW50IGk7CgogICAgZm9yIChpID0gMDsgaSA8IDE1OyBpKyspIHsKICAgICAgICBkW2ldID0gaSArIDE7CiAgICB9CgogICAgbmFyYWJlKGQsIDE1LCAxNSk7CiAgICBwcmludGYoIlxuIik7CiAgICByZXR1cm4gMDsKfQ==