#include "stdio.h"
#include "string.h"
#include "time.h"
#define SIZE 21
#define EMPTY SIZE
unsigned char perm[SIZE];
void fperm(unsigned char Number)
{
unsigned char i, j, count;
if (SIZE == Number)
{
char buff[1 /*\n*/ + 1/*{*/ + 4*SIZE - 2 + 1/*}*/ + 1/*0*/];
buff[0] = '\n';
buff[1] = '{';
for (i = 0; i < SIZE; i ++)
{
if (i != SIZE -1)
{
sprintf(buff + 2 + i * 4, "%2d, ", perm[ i]);
}
else
{
sprintf(buff + 2 + i * 4, "%2d", perm[ i]);
}
}
buff[1 + 1 + 4*SIZE - 2] = '}';
buff[1 + 1 + 4*SIZE - 2 + 1] = 0;
printf(buff);
}
else
{
for (i = 0; i < SIZE - Number; i ++)
{
count = 0;
for (j = 0; j < SIZE; j++)
{
if (EMPTY == perm[j])
{
if (count >= i)
{
perm[j] = Number;
fperm(Number + 1);
perm[j] = EMPTY;
break;
}
count ++;
}
}
}
}
}
int main(int argc, char* argv[])
{
time_t start, end;
memset(perm, EMPTY, SIZE);
time(&start);
fperm(0);
time(&end);
printf("\n\nExecution time in second: %ld", end - start);
getchar();
return 0;
}
P.S. аз бих сменил
#define SIZE 21
на примерно
#define SIZE 9
преди да пусна програмата :)Редактирано от Tweeg на 08.05.09 22:18.