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. LinkedHashMap和HashTable

    LinkedHashMap: 继承了HashMap: 其中,key不允许重复是Map接口就有的性质: HashTable: 同步的,意味着是单线程,意味着线程安全的,但是速度慢,和List接口集合的子 ...

  2. Linux 系统TCP连接内存大小限制 调优

    系统TCP连接内存大小限制 TCP的每一个连接请求,读写都需要占用系统内存资源,可根据系统配置,对TCP连接数,内存大小,限制调优. 查看系统内存资源 记录内存 详情:cat /proc/meminf ...

  3. D7 割点 割边 强连通分量

    今天几道是模板题: 第一道:(粘不了链接呜呜呜) 题目描述 n个城市之间有通讯网络,每个城市都有通讯交换机,直接或间接与其它城市连接.因电子设备容易损坏,需给通讯点配备备用交换机. 但备用 交换机数量 ...

  4. vc++2010如何新建项目并在控制台打印helloworld

    关于写c++使用什么集成开发环境的问题其实挺纠结的.我找了好久找到codeblocks,发现这款IDE还是最适合用在最标准的c++语法环境中.其实先前装过vs2015旗舰版,但是这款软件太大了,非常消 ...

  5. Pandas之分组

    假如我们现在有这样一组数据:星巴克在全球的咖啡店信息,如下图所示.数据来源:starbucks_store_locations.我们想要统计中国每个城市的星巴克商店的数量,那我们应该怎么做呢? 在pa ...

  6. Machine Learning--week3 逻辑回归函数(分类)、决策边界、逻辑回归代价函数、多分类与(逻辑回归和线性回归的)正则化

    Classification It's not a good idea to use linear regression for classification problem. We can use ...

  7. [POI1999][LOJ10112]原始生物

    典型的有向图K笔画的问题 最后答案就是n+1-1+k 1笔画有一点入度比出度少1 k笔画则统计入度比出度少的点中所有少的总和 #include<bits/stdc++.h> using n ...

  8. pt站 扫盲贴 面向小白

    1.什么是pt站? 大家应该知道种子,平时下电影的渠道除了百度云,就是种子了.这种普通的的种子称为bt. pt是私人种,和bt的区别是:不是谁想下就能下的,你得加入一个社区,入了伙,社区成员之间才能相 ...

  9. _pet

    可以控制各职业召唤物的属性.用于增强BB `comment` 备注 `classIndex` 职业序号 `DmgAddPct` 宠物伤害倍率 `SpAddPct` 法术伤害 `HpAddPct`血量倍 ...

  10. maven 如何使用

    以前没有用过maven管理过项目的依赖,最后使用上了maven,发现通过不能方式建立出来的web应用程序目录结构基本都不一样,既然每次都要到网上搜索如何建立maven管理的Web应用程序,不如自己找百 ...