Full Permutation


全排列问题, 将1~n这n个整数按字典序排放

  • 划分:

    • 输出1开头的全排列
    • 输出2开头的全排列
    • ......
    • 输出n开头的全排列
  • 递归边界:当下标1 ~ n 位都已经填好了,此时表示排列已经生成,可以输出。

  • 递归子式:即上述划分

//以3为例的全排列
#include <cstdio> const int maxn = 11;
int n, p[maxn], hashTable[maxn] = {false};
//p为当前排列,hashTable记录x是否在p中
void generateP(int index) {
if (index == n + 1) { //递归边界
for (int i = 1; i <= n; i++) {
printf("%d", p[i]);
}
printf("\n");
return;
}
for (int x = 1; x <= n; x++) {
if (hashTable[x] == false) {
p[index] = x;
hashTable[x] = true; //记录x已在p中
generateP(index + 1); //处理下一位
hashTable[x] = false; //已处理完p[index]为x的子问题,还原状态
}
}
} int main() {
n = 3;
generateP(1);
return 0;
}

Full permutation的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

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

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

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

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

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. Permutation test: p, CI, CI of P 置换检验相关统计量的计算

    For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...

  9. Permutation

    (M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II

  10. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

随机推荐

  1. xxnet to google部署

    1,github上下载xxnet项目 2,启动(点击 start) 3,确定启动好后访问 www.google.com (此时是可以访问的) 4,注册google账号或直接登陆 5,访问 https: ...

  2. vue项目中postcss-pxtorem的使用及webpack中的配置 css中单位px和em,rem的区别

    移动手机版要求我们在制作嵌入h5的时候去适配不同的手机.适配有多重模式,有flex.百分比等.字体大小的控制也有px.百分比.rem等单位,webpack中 px转rem. vue项目中postcss ...

  3. 一致性hash的实现

    原文:https://blog.csdn.net/u011305680/article/details/79721030 1.不带虚拟节点的 package hash; import java.uti ...

  4. JavaScript 字典

    JavaScript 字典 字典以 key value 形式出现 使用: a = {'k1':'v1,''k2':'v2'} 获取值: a['k1'] 获取值:v1

  5. mybatis 查询优化主子表查询之association和collection

    很多开发人员之所以编写出低效的应用,有一大原因是并不理解怎样编写高效的SQL.以订单查询为例,我们经常需要查询某个用户的订单以及订单明细,并且以树形方式展现如下: 对于这种性质的功能,很多开发人员的做 ...

  6. redhat6.4提权Ⅱ

    本次演示只针对redhat6.4, 其他的系统不知道有没有效果. 下面开始吧 建立普通用户并授予密码 [root@localhost yum.repos.d]# useradd test [root@ ...

  7. 【双目备课】OpenCV例程_stereo_calib.cpp解析

    stereo_calib是OpenCV官方代码中提供的最正统的双目demo,无论数据集还是代码都有很好实现. 一.代码效果: 相关的内容包括28张图片,1个xml和stereo_calib.cpp的代 ...

  8. 【Alpha】Scrum Meeting 1

    前言 第1次会议在3月17日由PM在教一317召开. 主要确定了选题,并对目标进行了初步的确定.时长90min. 任务分配 姓名 当前阶段任务 下阶段任务 吴昊 确定项目方向,本地部署代码,完成团队介 ...

  9. python 的运行方式和基础变量

    机器语言:直接用计算机能听的懂得二进制去编写程序,需要了解硬件细节 优点:执行效率高 缺点:开发效率低汇编语言:用英文单词取代二进制指令去编写程序 优点:开发效率高于机器语言 缺点:执行效率低于机器语 ...

  10. Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别

    Spring中ClassPathXmlApplication与FileSystemXmlApplicationContext的区别 一.概述 在项目中遇到加载不到Spring配置文件,简单分析后,写此 ...