全排列

思想:
     这是一个全排列问题,需要使用递归实现,将数组中的所有元素和第一个元素交换,求后面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. CodeForces - 632E Thief in a Shop (FFT+记忆化搜索)

    题意:有N种物品,每种物品有价值\(a_i\),每种物品可选任意多个,求拿k件物品,可能损失的价值分别为多少. 分析:相当于求\((a_1+a_2+...+a_n)^k\)中,有哪些项的系数不为0.做 ...

  2. Java堆内存设置

    转自:https://blog.csdn.net/Qiuzhongweiwei/article/details/81023645 堆内存设置 原理 JVM堆内存分为2块:永久空间和堆空间. 永久即持久 ...

  3. Android4.0 Surface机制分析

    1. java层面的Surface     对于Surface我们的认识主要是android的类Surface, android的文档描述Surface是"Handle onto a raw ...

  4. etcd:从应用场景到实现原理的全方位解读 转自infoq

    转自 infoq etcd:从应用场景到实现原理的全方位解读 http://www.infoq.com/cn/articles/etcd-interpretation-application-scen ...

  5. centos安装xdebug 和 phpstorm+Xdebug断点调试PHP

    转载地址:http://www.2cto.com/os/201304/206058.html   CentOS下安装xdebug   在CentOS 6.x 的系统中,是集成xdebug 的,   y ...

  6. bat调用exe文件并且传递参数

    bat调用exe文件并且传递参数 bat调用exe,并且传递日期参数,代码: @echo off cd "E:\SublimeWorks\exe" start xyzj_shrjj ...

  7. No module named _tkinter

    https://www.douban.com/note/524197380/?type=like

  8. mapper的namespace

    一般情况下mapper的namespace能随便写,不重复即可, 但如果希望使用mybatis动态代理的接口,就需要namespace中的值和需要对应的Mapper(dao)接口的全路径一致.例如:c ...

  9. nginx的坑-org.apache.http.TruncatedChunkException: Truncated chunk( expected size: 7752; actual size: 4077)

    org.apache.http.TruncatedChunkException: Truncated chunk 项目中使用请求远程接口报错 ,项目是Spring-boot的,两个项目(A和B) , ...

  10. RSA非对称加密算法

    基本定义: RSA公开密钥密码体制.所谓的公开密钥密码体制就是使用不同的加密密钥与解密密钥,是一种“由已知加密密钥推导出解密密钥在计算上是不可行的”密码体制.在公开密钥密码体制中,加密密钥(即公开密钥 ...