Advertisement
double_trouble

Untitled

Sep 30th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <math.h>
  8.  
  9. using namespace std;
  10.  
  11. void perest(long p[10]);
  12.  
  13. long p[10];
  14. char b[10];
  15. long m;
  16.  
  17. int main()
  18. {
  19.    freopen("input.txt","r", stdin);
  20.    freopen("output.txt", "w",stdout);
  21.    int n,j;
  22.    cin>>n>>m;
  23.    for (int i=1; i<=n; i++)
  24.         b[i] = 'A' + i - 1;
  25.    for (int i = 0; i<=m; i++)
  26.     p[i] = i;
  27.    do
  28.    {
  29.        perest(p);
  30.        j = m;
  31.        while (j>0 && p[j] == j+n-m){
  32.         cout<<p[j];
  33.         j--;
  34.        }
  35.        cout<<endl;
  36.        if (j>0)
  37.        {
  38.         p[j]++;
  39.         for (int i=j+1; i<=m; i++)
  40.         p[i] = p[i-1] + 1;
  41.        }
  42.    }
  43.    while (j>0);
  44. return 0;
  45. }
  46.  
  47. void perest(long p[10])
  48. {
  49.     long x,l,r,j,k;
  50.     while (p[0] == 0)
  51.     {
  52.         for (int i=1; i<=m; i++)
  53.             cout<<b[p[i]];
  54.         cout<<endl;
  55.         j = m;
  56.         while (p[j]<p[j-1])
  57.             j--;
  58.         k = m;
  59.         while (p[k]<p[j-1])
  60.             k--;
  61.         x = p[k];
  62.         p[k] = p[j-1];
  63.         p[j-1] = x;
  64.         l = j;
  65.         r = m;
  66.         while (l < r)
  67.         {
  68.             x = p[l];
  69.             p[l] = p[r];
  70.             p[r] = x;
  71.             l++;
  72.             r--;
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement