#include <stdio.h>
#include <stdlib.h>
void bubbleSort(int *a, int len)
{
for (int i = 0; i < len; ++i)
for (int j = i + 1; j < len; ++j)
if (a[i] < a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int func(int *a, int len, int n)
{
int *b
= malloc(sizeof(int) * len
), r
= 0, base
= 1; --n;
for (int i = 0; i < len; ++i)
b[i] = a[i];
bubbleSort(b, len);
for (int i = 2; i <= len; ++i)
base *= i;
for (; len; --len) {
base /= len;
int nn = (n / base) + 1;
int pos = 0;
for (int i = 0; nn; ++i) {
pos = i;
if (b[i] >= 0)
--nn;
}
r *= 10;
r += b[pos];
b[pos] = -1;
n %= base;
}
return r;
}
int main()
{
int a[] = {2, 3, 5, 7};
int len = sizeof(a) / sizeof(a[0]);
for (int i = 1; i <= 24; ++i)
printf("%d\n", func
(a
, len
, i
)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnZvaWQgYnViYmxlU29ydChpbnQgKmEsIGludCBsZW4pCnsKICBmb3IgKGludCBpID0gMDsgaSA8IGxlbjsgKytpKQogICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDwgbGVuOyArK2opCiAgICAgIGlmIChhW2ldIDwgYVtqXSkgewogICAgICAgIGludCB0ID0gYVtpXTsKICAgICAgICBhW2ldID0gYVtqXTsKICAgICAgICBhW2pdID0gdDsKICAgICAgfQp9CgppbnQgZnVuYyhpbnQgKmEsIGludCBsZW4sIGludCBuKQp7CgogIGludCAqYiA9IG1hbGxvYyhzaXplb2YoaW50KSAqIGxlbiksIHIgPSAwLCBiYXNlID0gMTsKICAtLW47CiAgZm9yIChpbnQgaSA9IDA7IGkgPCBsZW47ICsraSkKICAgIGJbaV0gPSBhW2ldOwogIGJ1YmJsZVNvcnQoYiwgbGVuKTsKICBmb3IgKGludCBpID0gMjsgaSA8PSBsZW47ICsraSkKICAgIGJhc2UgKj0gaTsKICBmb3IgKDsgbGVuOyAtLWxlbikgewogICAgYmFzZSAvPSBsZW47CiAgICBpbnQgbm4gPSAobiAvIGJhc2UpICsgMTsKICAgIGludCBwb3MgPSAwOwogICAgZm9yIChpbnQgaSA9IDA7IG5uOyArK2kpIHsKICAgICAgcG9zID0gaTsKICAgICAgaWYgKGJbaV0gPj0gMCkKICAgICAgICAtLW5uOwogICAgfQogICAgciAqPSAxMDsKICAgIHIgKz0gYltwb3NdOwogICAgYltwb3NdID0gLTE7CiAgICBuICU9IGJhc2U7CiAgfQogIHJldHVybiByOwp9CgppbnQgbWFpbigpCnsKICBpbnQgYVtdID0gezIsIDMsIDUsIDd9OwogIGludCBsZW4gPSBzaXplb2YoYSkgLyBzaXplb2YoYVswXSk7CiAgZm9yIChpbnQgaSA9IDE7IGkgPD0gMjQ7ICsraSkKICAgIHByaW50ZigiJWRcbiIsIGZ1bmMoYSwgbGVuLCBpKSk7CiAgcmV0dXJuIDA7Cn0K