全排列

作者: 优劣在于己 | 来源:发表于2020-11-09 20:49 被阅读0次

    对于函数prev_permutation()与next_permutation()全排列的用法
    直接观察输入输出,一目了然,用的时候记得给原数组排个序,具体你要用哪个,看自己吧

    对了!头文件是#include<algorithm>!!!


    函数prev_permutation()的用法:

    #include<stdio.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int main(){
        int arr[]={3,2,1};
        cout<<"用prev_permutation对123进行的全排序"<<endl;
        do{
            cout<<arr[0]<<arr[1]<<arr[2]<<endl;
        }while(prev_permutation(arr,arr+3));//大到小
    }
    
    输出:
    用prev_permutation对123进行的全排序
    321
    312
    231
    213
    132
    123
    

    函数next_permutation()的用法:
    #include<stdio.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    int main(){
        int arrl[]={1,2,3};
        cout<<"用next_permutation对123全排序"<<endl;
        do{
            cout<<arrl[0]<<arrl[1]<<arrl[2]<<endl;
        }while(next_permutation(arrl,arrl+3));//小到大
    }
    
    输出
    用next_permutation对123全排序
    123
    132
    213
    231
    312
    321
    

    相关文章

      网友评论

          本文标题:全排列

          本文链接:https://www.haomeiwen.com/subject/cwyrbktx.html