fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int n,x[1000];
  5. bool ok=true;
  6. void init(){
  7. cin>>n;
  8. for(int i=0;i<n;i++){
  9. x[i]=i+1;
  10. }
  11. }
  12. void result(){
  13. for(int i=0;i<n;i++){
  14. cout<<x[i]<<" ";
  15. }
  16. cout<<endl;
  17. }
  18. void next_permutation(){
  19. int j=n-2;
  20. while(j>=0&&x[j]>=x[j+1]){
  21. j--;
  22. }
  23. if(j>=0){
  24. int k=n-1;
  25. while(x[k]<=x[j]){
  26. k--;
  27. }
  28. swap(x[j],x[k]);
  29. reverse(x+j+1,x+n);
  30. }
  31. else ok=false;
  32.  
  33. }
  34. int main(){
  35. init();
  36. while(ok){
  37. result();
  38. next_permutation();
  39. }
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout