美文网首页
递归-全排列

递归-全排列

作者: Co_zy | 来源:发表于2018-04-03 18:33 被阅读0次

对数组{1,2,3}进行全排列

#include <iostream>
using namespace std;
void swap(int a[],int i,int j)
{
    int tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
}
void printArray(int a[],int n)
{
    for(int i=0; i<n; i++)
        cout<<a[i]<<" ";
    cout<<endl;
}
void perm(int a[],int p,int q)
{
    if(p==q)
        printArray(a,q+1);
    for(int i=p; i<=q; i++)
    {
        swap(a,p,i);
        perm(a,p+1,q);
        swap(a,p,i);
    }
}
int main()
{
    int a[] = {1,2,3};
    perm(a,0,2);
    return 0;
}

STL next_permutation

#include <algorithm>
int main()
{
    int a[] = {1,2,3};
    //perm(a,0,2);
    do
    {
        for(int i=0;i<3;i++)
            cout<<a[i]<<" ";
        cout<<endl;
    }while(next_permutation(a,a+3));
    return 0;
}

相关文章

  • 全排列与字典序

    全排列 递归实现全排列; 首先来说递归算法实现全排列: 例如,对于{1,2,3,4}的例子进行全排列,其可以分解...

  • 递归-全排列

    对数组{1,2,3}进行全排列 STL next_permutation

  • 全排列与全组合

    递归+交换值实现全排列 非重复的全排列 位运算实现全组和

  • 全排列

    求全排列最简单的就是递归了123 的全排列共有 6 个, 123 的全排列等于以 1 开头 23 的全排列, 加上...

  • 关于数组的一些操作【python】

    递归的应用:求输入字符串的全排列 求输入字符串的全排列递归完成,也可以直接使用库函数 结果展示: ————————...

  • 全排列(数字重复与不重复)问题的递归与非递归代码

    全排列给定一个数字列表,返回其所有可能的排列。 样例给出一个列表[1,2,3],其全排列为: 递归代码 非递归代码...

  • 排列

    上述代码中列出了 全排列的非递归、递归解法 从n个数中取m个的各种排列形式输出

  • 递归--全排列问题

    前置文章:递归算法:www.jianshu.com/p/703069f3ba3f . 递归问题有两个经典的...

  • 递归-8 全排列

    // //ViewController.m //CocoTest_1 // //Created by S u p ...

  • 全排列(递归算法)

    一. 全排列算法 首先:什么是全排列=》百度一下 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,...

网友评论

      本文标题:递归-全排列

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