全排列

思想:
     这是一个全排列问题,需要使用递归实现,将数组中的所有元素和第一个元素交换,求后面n-1个元素的全排列。
     按照这个条件递归下去,知道元素的个数只有一个的时候,输出所有的元素。
 
#include <iostream>
using namespace std;
 
int total = 0;
void perm(int arr[], int start , int size){
                 if (start >= size){
                                 for (int i = 0; i < size; i++){
                                                cout << arr[i] << " " ;
                                }
                                cout << endl;
                                total++;
                                 //return;
                }
                 else{
                                 for (int i = start; i < size; i++){
                                                 int temp = arr [start];
                                                 arr[start ] = arr[i];
                                                 arr[i] = temp;
 
                                                perm( arr, start + 1, size);
 
                                                temp = arr[start ];
                                                 arr[start ] = arr[i];
                                                 arr[i] = temp;
                                }
                }
}
 
int main(){
                 int a [] = { 1, 2, 3, 4, 5, 6 };
                perm(a, 0, 3);
                cout<< total << endl;
                 return 0;
}
 
此外在C++STL中也有一个关于全排列的函数:
     next_permuntation(start, start_length);
     int a [] = { 1, 2, 3, 4, 5, 6 };
                sort(a, a + 6);
                 while (next_permutation(a, a + 6)){
                                 for (int i = 0; i < 6; i++){
                                                cout << a[i] << " " ;
                                }
                                cout << endl;
                }

Algorithm1: 全排列的更多相关文章

  1. PHP实现全排列(递归算法)

    算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为:    ① 如果n=1,则排列P只有一 ...

  2. hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)

    xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串.                      (题于文末) 知识点: n个元素,其中a1,a2,··· ...

  3. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  5. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  7. 全排列算法的JS实现

    问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...

  8. java实现全排列

    前天上午的面试遇到了一个用java实现一串数字的全排列的题,想来想去用递归最方便,可是没有在规定的时间内完成555,今天上午有空便继续写,以下是完成后的代码: import java.util.Arr ...

  9. poj3187-Backward Digit Sums(枚举全排列)

    一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角)  3 1 2 4 //1~n 全排列中的一个排列  4 3 6  7 ...

随机推荐

  1. 关于hibernate插入数据到mysql数据库中文乱码问题的解决

    要想解决这个问题就要找到问题的症结所在 1.首先将数据提交到action输出看action里的数据是不是中文乱码,结果很遗憾并不是这里的问题 2.设置数据库连接url: 3.打开mysql安装文件里的 ...

  2. vux在ISO中异常 this.$vux.confirm.show

    在按钮事件中调用this.$vux.confirm.show,并且启用按钮的show-loading属性 安卓正常,ios中弹窗无法显示 经过排查,iso中设置按钮的loading后,要用异步setT ...

  3. jQuery判断元素是否显示 是否隐藏

    var node=$('#id'); 第一种写法 if(node.is(':hidden')){ //如果node是隐藏的则显示node元素,否则隐藏 node.show(); }else{ node ...

  4. params

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch06 ...

  5. 解读:hadoop压缩格式

    Hadoop中用得比较多的4种压缩格式:lzo,gzip,snappy,bzip2.它们的优缺点和应用场景如下: 1). gzip压缩 优点:压缩率比较高,而且压缩/解压速度也比较快:hadoop本身 ...

  6. git-修改远程的URL

    git remote set-url命令修改remote URL git remote set-url传递两个参数 remote name.例如,origin或者upstream new remote ...

  7. 100个gdb调试技巧

    找到的一个有参考价值的关于GDB调试的站点:https://gitlore.com/subject/15

  8. RabbitMQ脑裂

    在RabbitMQ3.4.x中会出现脑裂的现象,本文通过实验验证此脑裂现象,愿小伙伴们少走弯路. Preview 网上有两篇帖子(需要FQ) https://groups.google.com/for ...

  9. cp 复制 mv剪切

    cp cp -p test.rb /home/test    将test.rb copy到test目录,并且保留原文件的属性cp -r Dir/ /home/test    将Dir目录copy到te ...

  10. mysql常见知识点总结

    mysql常见知识点总结 参考: http://www.cnblogs.com/hongfei/archive/2012/10/20/2732516.html https://www.cnblogs. ...