3 solutions

  • 5
    @ 2024-7-16 20:38:34

    题意

    寻找下 kk 个按字典序排的全排列

    思路

    使用 next_permutation()[1] 自动寻找下一个全排列

    代码

    #include <bits/stdc++.h>
    using namespace std;
    int a[10005];
    int main(int argc, char **argv){
    	int n,k;
    	cin >> n >> k;
    	for (int i = 0;i < n;i++){
    		cin >> a[i];
    	}
    	for (int i = 0;i < k;i++){
    		next_permutation(a,a + n);
    	}
    	for (int i = 0;i < n;i++){
    		printf("%d ",a[i]);
    	}
    	return 0;
    }
    

    1. next_permutation(first,last):将 [first, last) 的元素换到下一个全排列 ↩︎

    • 1
      @ 2024-7-17 18:42:31
      #include <bits/stdc++.h>
      using namespace std;
      int a[1000005];
      int main(){
      	int  n,k;//代码来自pwm本人做了注解鸣谢 
      	cin>>n>>k;
      	for(int i=0;i<n;i++){
      		cin>>a[i];//前面都很简单 
      	}
      	for(int i=0;i<k;i++){
      		next_permutation(a,a+n) ;//这里就是看本次的数据编号+n表示的数的排列 
      		//这里要按照大小排列 全排列
      	//这个函数是寻找下一个全排列 
      		//附一个样例解释 应该可以理解 12345表示的是编号1 12354编号 2 12435为3 1+3=4 
      	//编号4的就是12453
      	}
      	for(int i=0;i<n;i++){
      		cout<<a[i]<<" ";
      	}
      	return 0;
      }
      
    • 0
      @ 2024-12-18 13:39:52

      手写全排列思路

      //这里给一段调试代码 
      #include<bits/stdc++.h>
      using namespace std;
      #define ll long long
      const int maxn=1E4+10,step_num=4;
      int a[maxn],n,m; 
      int main(){
      	cin>>n>>m;
      	for(int i=0;i<n;i++){
      		cin>>a[i]; 
      	}
      	while(next_permutation(a,a+n)){
      		for(int i=0;i<n;i++){
      			cout<<a[i]<<" "; 
      		}
      		cout<<endl;
      	}
      	return 0;
      }
      /*
      //思考手写next_permutation规律 
      1 2 3 4 5
      1 2 3 5 4// 4 5->5 4
      1 2 4 3 5// 3->4 剩下5 3要逆转变成升序3 5(字典序最小) 
      1 2 4 5 3// 3 5->5 3
      1 2 5 3 4
      1 2 5 4 3
      1 3 2 4 5// 2->3 剩下5 4 2要逆转变成升序2 4 5(字典序最小)
      1 3 2 5 4
      1 3 4 2 5
      1 3 4 5 2
      1 3 5 2 4
      1 3 5 4 2// 5 4 2以满足降序(字典序最大) 
      1 4 2 3 5//把 5 4 2中最小的大于3的数挑出来和3换 
      1 4 2 5 3
      1 4 3 2 5
      1 4 3 5 2
      1 4 5 2 3
      1 4 5 3 2
      1 5 2 3 4
      1 5 2 4 3
      1 5 3 2 4
      1 5 3 4 2
      1 5 4 2 3
      1 5 4 3 2
      2 1 3 4 5
      2 1 3 5 4
      2 1 4 3 5
      2 1 4 5 3
      2 1 5 3 4
      2 1 5 4 3
      2 3 1 4 5
      2 3 1 5 4
      2 3 4 1 5
      2 3 4 5 1
      2 3 5 1 4
      2 3 5 4 1
      2 4 1 3 5
      2 4 1 5 3
      2 4 3 1 5
      2 4 3 5 1
      2 4 5 1 3
      2 4 5 3 1
      2 5 1 3 4
      2 5 1 4 3
      2 5 3 1 4
      2 5 3 4 1
      2 5 4 1 3
      2 5 4 3 1
      3 1 2 4 5
      3 1 2 5 4
      3 1 4 2 5
      3 1 4 5 2
      3 1 5 2 4
      3 1 5 4 2
      3 2 1 4 5
      3 2 1 5 4
      3 2 4 1 5
      3 2 4 5 1
      3 2 5 1 4
      3 2 5 4 1
      3 4 1 2 5
      3 4 1 5 2
      3 4 2 1 5
      3 4 2 5 1
      3 4 5 1 2
      3 4 5 2 1
      3 5 1 2 4
      3 5 1 4 2
      3 5 2 1 4
      3 5 2 4 1
      3 5 4 1 2
      3 5 4 2 1
      4 1 2 3 5
      4 1 2 5 3
      4 1 3 2 5
      4 1 3 5 2
      4 1 5 2 3
      4 1 5 3 2
      4 2 1 3 5
      4 2 1 5 3
      4 2 3 1 5
      4 2 3 5 1
      4 2 5 1 3
      4 2 5 3 1
      4 3 1 2 5
      4 3 1 5 2
      4 3 2 1 5
      4 3 2 5 1
      4 3 5 1 2
      4 3 5 2 1
      4 5 1 2 3
      4 5 1 3 2
      4 5 2 1 3
      4 5 2 3 1
      4 5 3 1 2
      4 5 3 2 1
      5 1 2 3 4
      5 1 2 4 3
      5 1 3 2 4
      5 1 3 4 2
      5 1 4 2 3
      5 1 4 3 2
      5 2 1 3 4
      5 2 1 4 3
      5 2 3 1 4
      5 2 3 4 1
      5 2 4 1 3
      5 2 4 3 1
      5 3 1 2 4
      5 3 1 4 2
      5 3 2 1 4
      5 3 2 4 1
      5 3 4 1 2
      5 3 4 2 1
      5 4 1 2 3
      5 4 1 3 2
      5 4 2 1 3
      5 4 2 3 1
      5 4 3 1 2
      5 4 3 2 1
      */
      
      
      • 1

      Information

      ID
      88
      Time
      1000ms
      Memory
      125MiB
      Difficulty
      2
      Tags
      # Submissions
      69
      Accepted
      28
      Uploaded By