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. rocket mq知识点

    1 消费类型 广播消费 : 一条消息被多个消费者消费 集群消费:一个 Consumer Group 中的 Consumer 实例平均分摊消费消息.例如某个 Topic 有 9 条消息,其中一个 Con ...

  2. Kube-DNS搭建(1.4版本)

    目录贴:Kubernetes学习系列 1.介绍 之前介绍过DNS的搭建(基于Kubernetes集群部署skyDNS服务),但那个版本的DNS是随着Kubernetes1.2发布出来的,有点原始.本文 ...

  3. 移动端热更新方案(iOS+Android)

    PPT资源包含iOS+Android 各种方案分析:https://github.com/qiyer/Share/blob/master/%E7%83%AD%E6%9B%B4%E6%96%B0%E5% ...

  4. 20190404用户及用户组管理(week1_day4)

    useradd userdel usermod groupadd groupdel 用户管理 为什么需要有用户? 1. linux是一个多用户系统 2. 权限管理(权限最小化) 用户:存在的目录是为了 ...

  5. Node.js基础学习一之Get请求

    本人从事的是前端开发,这段时间公司开发项目比较少所以就想着学点东西,然后就想到了Node.js ,跟着菜鸟教程学了点,不过我觉得最好的学习方法是带着需求来学习. 其实和服务端打交道无非就是能有一个可以 ...

  6. oracle(3)

    存储过程: CREATE OR REPLACE PROCEDURE PROC_ABC ( currency IN NUMBER, aysdate IN DATE, money OUT number ) ...

  7. while(scanf("%d %d",&a,&b)!=EOF)

    scanf("%d %d",&a,&b)返回输入的数据和格式字符串中匹配次数.当dos或windows中输入ctrl+z(模拟文件结束符EOF)时,scanf返回E ...

  8. linux基础之grep

    grep: Global search REgular expression and Print out the line 作用: 文本搜索工具,根据用户指定的模式对目标文本逐行进行匹配检查,打印匹配 ...

  9. 【做题】SDOI2017硬币游戏——方程&概念处理

    原文链接 https://www.cnblogs.com/cly-none/p/9825339.html 题意:给出\(n\)个长度为\(m\)的互不相同的01串.有另一个串,初始为空.不断进行如下操 ...

  10. 使用C#控制台应用程序完成一个2048小游戏

    曾经使用C#控制台应用程序写的一个2048,现在翻出来回顾一下 Box类是2048中每一个小格子的相关信息,包括格子的横纵坐标.格子的值和格子是否刚合并这些信息. Grid类是网格的相关信息,包括网格 ...