#include <stdio.h>
char *input_argv[] = { "prog.c", "12", "10", "17", "1", "22" }; // al posto di argv[]
int input_argc = sizeof (input_argv) / sizeof (input_argv[0]); // al posto di argc
void visualizza (int lista[], int dimensione)
{
int i;
for (i = 0; i < dimensione; i++)
{
}
}
void permuta (int lista[], int a, int z, int dimensione)
{
int scambio;
int k;
// Se il segmento di array contiene almeno due elementi,
// si procede.
if ((z - a) >= 1)
{
// Inizia un ciclo di scambi tra l'ultimo elemento e
// uno degli altri contenuti nel segmento di array.
for (k = z; k >= a; k--)
{
// Scambia i valori.
scambio = lista[k];
lista[k] = lista[z];
lista[z] = scambio;
// Esegue una chiamata ricorsiva per permutare
// un segmento piĆ¹ piccolo dell'array.
permuta (lista, a, z - 1, dimensione);
// Scambia i valori.
scambio = lista[k];
lista[k] = lista[z];
lista[z] = scambio;
}
}
else
{
// Visualizza l'array.
visualizza (lista, dimensione);
}
}
int
main (int argc, char *argv[])
{
int lista[input_argc - 1];
int i;
// Considera gli argomenti come gli elementi
// dell'array da permutare.
for (i = 1; i < input_argc; i++)
{
sscanf (input_argv
[i
], "%i", &lista
[i
-1]); }
// Esegue le permutazioni.
permuta (lista, 0, input_argc - 2, input_argc - 1);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiAKY2hhciAqaW5wdXRfYXJndltdID0geyAicHJvZy5jIiwgIjEyIiwgIjEwIiwgIjE3IiwgIjEiLCAiMjIiIH07ICAgICAvLyBhbCBwb3N0byBkaSBhcmd2W10KaW50ICAgaW5wdXRfYXJnYyAgID0gc2l6ZW9mIChpbnB1dF9hcmd2KSAvIHNpemVvZiAoaW5wdXRfYXJndlswXSk7ICAvLyBhbCBwb3N0byBkaSBhcmdjCgp2b2lkIHZpc3VhbGl6emEgKGludCBsaXN0YVtdLCBpbnQgZGltZW5zaW9uZSkKewogICAgaW50IGk7CgogICAgZm9yIChpID0gMDsgaSA8IGRpbWVuc2lvbmU7IGkrKykKICAgICAgewogICAgICAgIHByaW50ZiAoIiVpICIsIGxpc3RhW2ldKTsKICAgICAgfQogICAgcHJpbnRmICgiXG4iKTsKfQoKdm9pZCBwZXJtdXRhIChpbnQgbGlzdGFbXSwgaW50IGEsIGludCB6LCBpbnQgZGltZW5zaW9uZSkKewogICAgaW50IHNjYW1iaW87CiAgICBpbnQgazsKCiAgICAvLyBTZSBpbCBzZWdtZW50byBkaSBhcnJheSBjb250aWVuZSBhbG1lbm8gZHVlIGVsZW1lbnRpLAogICAgLy8gc2kgcHJvY2VkZS4KCiAgICBpZiAoKHogLSBhKSA+PSAxKQogICAgICB7CiAgICAgICAgLy8gSW5pemlhIHVuIGNpY2xvIGRpIHNjYW1iaSB0cmEgbCd1bHRpbW8gZWxlbWVudG8gZQogICAgICAgIC8vIHVubyBkZWdsaSBhbHRyaSBjb250ZW51dGkgbmVsIHNlZ21lbnRvIGRpIGFycmF5LgoKICAgICAgICBmb3IgKGsgPSB6OyBrID49IGE7IGstLSkKICAgICAgICAgIHsKICAgICAgICAgICAgLy8gU2NhbWJpYSBpIHZhbG9yaS4KCiAgICAgICAgICAgIHNjYW1iaW8gPSBsaXN0YVtrXTsKICAgICAgICAgICAgbGlzdGFba10gPSBsaXN0YVt6XTsKICAgICAgICAgICAgbGlzdGFbel0gPSBzY2FtYmlvOwoKICAgICAgICAgICAgLy8gRXNlZ3VlIHVuYSBjaGlhbWF0YSByaWNvcnNpdmEgcGVyIHBlcm11dGFyZQogICAgICAgICAgICAvLyB1biBzZWdtZW50byBwacO5IHBpY2NvbG8gZGVsbCdhcnJheS4KCiAgICAgICAgICAgIHBlcm11dGEgKGxpc3RhLCBhLCB6IC0gMSwgZGltZW5zaW9uZSk7CgogICAgICAgICAgICAvLyBTY2FtYmlhIGkgdmFsb3JpLgoKICAgICAgICAgICAgc2NhbWJpbyA9IGxpc3RhW2tdOwogICAgICAgICAgICBsaXN0YVtrXSA9IGxpc3RhW3pdOwogICAgICAgICAgICBsaXN0YVt6XSA9IHNjYW1iaW87CiAgICAgICAgICB9CiAgIC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