2 solutions

  • 2
    @ 2024-5-5 9:08:47
    #include<bits/stdc++.h>
    using namespace std;
    const int N=20;
    int a[N];
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++) a[i]=n-i+1;
        for(int i=1;i<=n;i++) printf("%d ",a[i]);
        printf("\n");
        while(true)
        {
            int k=n;
            while(k-1>=1&&a[k-1]<a[k]) k--; //找到上升的位置
            if(k-1==0) break; //越界了
            k=k-1; //上升的位置 
            int t=n; //从后找到第一个大于它的元素
            while(a[k]<a[t]) t--; //小于就一直累加
            swap(a[k],a[t]); //和最后一个比他大的交换
            reverse(a+k+1,a+n+1);    //逆序
            for(int i=1;i<=n;i++)
            {
                printf("%d ",a[i]);
            }
            printf("\n");
        }
        return 0;
    }
    
    • 1
      @ 2026-1-18 16:03:07
      #include<bits/stdc++.h>
      using namespace std;
      int n;
      int main(){
      	cin>>n;
      	vector<int> v;
      	for(int i=n;i>=1;i--){
      		v.push_back(i);
      	}
      	do{
      		for(auto x:v){
      			cout<<x<<' ';
      		}
      		cout<<endl;
      	}while(prev_permutation(v.begin(),v.end()));
      	return 0;
      }
      
      
      • 1

      Information

      ID
      1278
      Time
      1000ms
      Memory
      256MiB
      Difficulty
      3
      Tags
      # Submissions
      31
      Accepted
      11
      Uploaded By