fork download
  1. #include<iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int n,x[10],ok;
  6. void khoitao(){
  7. cin>>n;
  8. for(int i=1;i<=n;i++) x[i]=n-i+1;
  9. ok=0;
  10. }
  11. void in(){
  12. for(int i=1;i<=n;i++) cout<<x[i];
  13. cout<<' ';
  14. }
  15. void sinh(){
  16. int d=n-1;
  17. while(d>0&&x[d]<x[d+1]) d--;
  18. if(d==0) {
  19. ok=1;
  20. }else{
  21. int j=n;
  22. while(x[j]>x[d]) j--;
  23. swap(x[j],x[d]);
  24. int l=d+1,r=n;
  25. while(l<r){
  26. swap(x[l],x[r]);
  27. l++;r--;
  28. }
  29. }
  30. }
  31. void xuli(){
  32. khoitao();
  33. while(ok==0){
  34. in();
  35. sinh();
  36. }
  37. }
  38. int main(){
  39. int t;cin>>t;
  40. while(t--){
  41. xuli();
  42. cout<<endl;
  43. }
  44. }
Success #stdin #stdout 0.01s 5532KB
stdin
2
2
4
stdout
21 12 
4321 4312 4231 4213 4132 4123 3421 3412 3241 3214 3142 3124 2431 2413 2341 2314 2143 2134 1432 1423 1342 1324 1243 1234