全排列

思想:
     这是一个全排列问题,需要使用递归实现,将数组中的所有元素和第一个元素交换,求后面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. 如何高效的遍历HashMap 以及对key 进行排序

    Map<Integer ,Object> map = new HashMap<Integer,Object>(); for(int i = 0; i<=100;i++){ ...

  2. HBase1.2.6 预分区后,数据不进入预定分区的一个 bug

    rowkey 如下: 19000015115042900001511504390000151150449000015115045900001511504690000151150479000015115 ...

  3. Twitter的分布式自增ID算法snowflake

    snowflake 分布式场景下获取自增id git:https://github.com/twitter/snowflake 解读: http://www.cnblogs.com/relucent/ ...

  4. Python中正则模块re.compile、re.match及re.search函数用法

    import rehelp(re.compile)'''输出结果为:Help on function compile in module re: compile(pattern, flags=0) C ...

  5. 关于MVC打印问题,打印指定的内容

    首先你的内容一定要放在一个div中如下代码 <div id="divprint"> <table class="table table-striped ...

  6. SublimeText3 编辑器使用小结

    1. 快捷键: Command + shift + D : 复制当前行 Command + shift + K : 删除当前行 Command + J : 合并一行 Command + Enter : ...

  7. [转载]Javassist 使用指南(一)

    ======================= 本文转载自简书,感谢原作者!. 原链接如下:https://www.jianshu.com/p/43424242846b =============== ...

  8. log4j:ERROR A "org.apache.log4j.DailyRollingFileAppender" object is not assignable to a "org.apache.log4j.Appender" variable.

    多个classloader加载log4j时需要设置当前Thread的classloader为你自己的classloader Thread.currentThread().setContextClass ...

  9. 【网络结构】Deep Residual Learning for Image Recognition(ResNet) 论文解析

    目录 0. 论文链接 1. 概述 2. 残差学习 3. Identity Mapping by shortcuts 4. Network Architectures 5. 训练细节 6. 实验 @ 0 ...

  10. Lubuntu系统中java,tomcat的环境搭建(virtualbox中)

    一.安装Lubuntu系统 这一步没什么说的,到官网下载镜像,在virtualbox中安装即可安装时就已经可以选择安装源,当然,选中国的设置环装网络,可将该虚拟机设立为网络上的独立IP,和物理机间可以 ...