Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <malloc.h>
- #include <conio.h>
- int fact(int n)
- {
- if (n == 1) return 1;
- return (n * fact(n - 1));
- }
- int main()
- {
- int i, l, j, k, buf, n, nfact;
- scanf("%d", &n);
- int *a = (int*)malloc(sizeof(int)*n);
- printf(" 1: ");
- for (i = 0; i < n; i++)
- {
- a[i] = i + 1;
- printf("%d ", a[i]);
- }
- nfact = fact(n);
- for (l = 0; l < nfact - 1; l++)
- {
- for (i = n - 2; i >= 0; i--)
- {
- if (a[i] < a[i + 1])
- {
- for (j = n - 1; j > i; j--)
- {
- if (a[j] > a[i])
- {
- buf = a[j];
- a[j] = a[i];
- a[i] = buf;
- break;
- }
- }
- for (k = i + 1; k < n; k++)
- {
- for (j = k + 1; j < n; j++)
- {
- if (a[k] > a[j])
- {
- buf = a[j];
- a[j] = a[k];
- a[k] = buf;
- }
- }
- }
- break;
- }
- }
- printf("\n%2d: ", l + 2);
- for (k = 0; k < n; k++)
- printf("%d ", a[k]);
- }
- free(a);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement