dzungchaos

CTDL&TT: Sinh các hoán vị

May 31st, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include<iostream>
  2. #define MAX 20
  3. using namespace std;
  4.  
  5. int n, count;
  6. int Bool[MAX] = { 0 };
  7. int A[MAX];
  8.  
  9. void xuat() {
  10.     cout << endl << ++count << ": ";
  11.     for (int i = 1; i <= n; i++)
  12.         cout << A[i] << " ";
  13.     cout << endl;
  14. }
  15.  
  16. void Try(int k) {
  17.     for (int i = 1; i <= n; i++) {
  18.         if (!Bool[i]) {
  19.             A[k] = i;
  20.             Bool[i] = 1;
  21.             if (k == n)
  22.                 xuat();
  23.             else
  24.                 Try(k + 1);
  25.             Bool[i] = 0;
  26.         }
  27.     }
  28. }
  29.  
  30. int main() {
  31.     cout << "Nhap n: ";
  32.     cin >> n;
  33.     cout << "Cac hoan vi: " << endl;
  34.     Try(1);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment