Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <stdlib.h>
- typedef unsigned int byte;
- byte N, i, j;
- byte *X;
- bool Yes;
- void Next(byte **A, bool *Y);
- void Swap(byte *a, byte *b);
- int _tmain(int argc, _TCHAR* argv[])
- {
- printf("N="); scanf_s("%d", &N);
- N++;
- X = (byte *)malloc(N*sizeof(byte));
- for (i = 0; i<N; i++) X[i] = i;
- do {
- for (i = 1; i<N; i++)
- printf("%d", X[i]);
- putchar('\n');
- Next(&X, &Yes);
- } while (Yes);
- return 0;
- }
- void Next(byte **A, bool *Y)
- {
- byte i, M = N - 1;
- byte *B = *A;
- i = M - 1;
- //пошук i
- while ((i >= 0) && (B[i]>B[i + 1])) i--;
- if (i>0) {
- j = i + 1;
- //пошук j
- while ((j<M) && (B[j + 1]>B[i]))
- j++;
- Swap(&X[i], &X[j]);
- for (j = i + 1; j <= (M + i) / 2; j++)
- Swap(&X[j], &X[M - j + i + 1]);
- *Y = true;
- }
- else *Y = false;
- }
- void Swap(byte *a, byte *b)
- {
- //обмін змінних
- byte c;
- c = *a;
- *a = *b;
- *b = c;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement