全排列

思想:
     这是一个全排列问题,需要使用递归实现,将数组中的所有元素和第一个元素交换,求后面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. enumerateObjectsUsingBlock 、for 、for(... in ...) 的区别 & 性能测试

    for VS for(... in ...) for 的应用范围广基本可以NSArray.NSArray以及C语言的数组等,而for(... in ...)仅限于NSArray.NSArray等 fo ...

  2. C#创建类,方法,接口,字段 的 默认类型

    1.在namespace中的类.接口默认是internal类型的,也可以显示的定义为public类型2.在一个类里面,属性和方法默认是private的,可以显示的定义为public.private.p ...

  3. 20145307第七周JAVA学习报告

    20145307<Java程序设计>第七周学习总结 教材学习内容总结 Lambda Lambda语法概述: Arrays的sort()方法可以用来排序,在使用sort()时,需要操作jav ...

  4. ubuntu如何释放内存

    答: step 1: 以最高权限同步所有的缓存到磁盘中 sync sync step2: 执行以下命令指示内核对内存进行调整 echo 3 > /proc/sys/vm/drop_caches ...

  5. 混合开发的大趋势之一React Native之简单的登录界面

    转载请注明出处:王亟亟的大牛之路 这些天都在学习RN这部分吧,然后写了个简单的登陆业务,从"实战"中讲解吧 还是继续安利:https://github.com/ddwhan0123 ...

  6. System.IO命名空间下常用的类

    System.IO System.IO.Directory 目录 System.IO.Path 文件路径(包含目录和文件名) System.IO.FileInfo 提供创建.复制.删除.移动和打开文件 ...

  7. LeetCode——minimum-path-sum

    Question Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  8. 客户端发一个post请求

    public static String doPostStr(String httpUrl, String str) { HttpPost httpPost = null; try { HttpCli ...

  9. spark SQL学习(数据源之json)

    准备工作 数据文件students.json {"id":1, "name":"leo", "age":18} {&qu ...

  10. jQuery loop over JSON字符串 – $.each实例

    先来一段简单的javascript对象的遍历: var json = [ {"id":"1","tagName":"apple&q ...