zoukankan      html  css  js  c++  java
  • 全排列函数(next_permutation)

    顾名思义,这个函数就是用来求数组的全排列的,至于怎么用,看下面的介绍:

    这是一个c++函数,包含在头文件algorithm里面,这个函数可以从当前的数组的大小按照字典序逐个递增的顺序排列

    看下面的模板

    int a[];
    do
    {
     
    }while(next_permutation);

    下面代码可以输出1~n的全排列

    #include <stdio.h>
    #include <algorithm>
    using namespace std;
    int main(){
        int n;
        while(scanf("%d",&n)&&n){
            int a[1000];
            for(int i=0;i<n;i++){
                scanf("%d",&a[i]);
            }
            sort(a,a+n);//可以自行测试一下删除后的结果
            do{
                for(int i=0;i<n;i++)
                    printf("%d ",a[i]);
                printf("
    ");
            }while(next_permutation(a,a+n));
        }
        return 0;
    }

    上面代码有一个sort,但是如果没有的话,只会出现从当前的大小按字典序逐个增加的排序

    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    FormData的使用
    数据绑定
    DOM的映射机制
    leetcode750
    leetcode135
    leetcode41
    leetcode269
    leetcode253
    leetcode42
    leetcode48
  • 原文地址:https://www.cnblogs.com/caijiaming/p/9431645.html
Copyright © 2011-2022 走看看