Advertisement
andruhovski

Untitled

Nov 12th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #include <stdlib.h>
  4.  
  5. typedef unsigned int byte;
  6.  
  7. byte N, i, j;
  8.  
  9. byte *X;
  10.  
  11. bool Yes;
  12.  
  13.  
  14. void Next(byte **A, bool *Y);
  15.  
  16. void Swap(byte *a, byte *b);
  17.  
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21. printf("N="); scanf_s("%d", &N);
  22.  
  23. N++;
  24.  
  25. X = (byte *)malloc(N*sizeof(byte));
  26.  
  27. for (i = 0; i<N; i++) X[i] = i;
  28.  
  29. do {
  30.  
  31. for (i = 1; i<N; i++)
  32.  
  33. printf("%d", X[i]);
  34.  
  35. putchar('\n');
  36.  
  37. Next(&X, &Yes);
  38.  
  39. } while (Yes);
  40.  
  41. return 0;
  42.  
  43.  
  44.  
  45. }
  46.  
  47.  
  48. void Next(byte **A, bool *Y)
  49. {
  50. byte i, M = N - 1;
  51. byte *B = *A;
  52. i = M - 1;
  53. //пошук i
  54. while ((i >= 0) && (B[i]>B[i + 1])) i--;
  55. if (i>0) {
  56. j = i + 1;
  57. //пошук j
  58. while ((j<M) && (B[j + 1]>B[i]))
  59. j++;
  60. Swap(&X[i], &X[j]);
  61. for (j = i + 1; j <= (M + i) / 2; j++)
  62. Swap(&X[j], &X[M - j + i + 1]);
  63. *Y = true;
  64. }
  65. else *Y = false;
  66. }
  67.  
  68.  
  69. void Swap(byte *a, byte *b)
  70. {
  71. //обмін змінних
  72. byte c;
  73. c = *a;
  74. *a = *b;
  75. *b = c;
  76. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement