Full permutation
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的更多相关文章
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 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 ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- 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 ...
- Permutation
(M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II
- Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
随机推荐
- linux下目录的作用
FHS针对目录树架构仅定义出三层目录底下应该放置什么数据而已,分别是底下这三个目录的定义: / (root, 根目录):与开机系统有关: /usr (unix software resource):与 ...
- api测试工具
在线接口测试 http://www.36nu.com/apiTest 使用Fiddler测试WebApi接口 https://www.cnblogs.com/weixing/p/5254836.htm ...
- URIError: Failed to decode param '/%PUBLIC_URL%/favicon.ico'
今天搭建antd的项目结构,本来项目是一个基础react项目,结果执行 yarn create umi yarn yarn start 项目启动后访问突然报错URIError: Failed to d ...
- Reading Lines from File in C++
Reading Lines from File in C++ In C++, istringstream has been used to read lines from a file. code: ...
- java程序员面试交流项目经验
粘贴自:https://blog.csdn.net/wangyuxuan_java/article/details/8778211 1:请你介绍一下你自己 这是面试官常问的问题.一般人回答这个问题过于 ...
- (转载)UnityShader学习笔记(七) 让贴图纹理动起来(河流瀑布特效、精灵序列帧实现)
大家好,我是Zander.这一章我们将使用纹理贴图实现动画.混合和真实特效来达到理想的效果. 纹理贴图可以使我们的着色器快速的实现逼真的效果,但是如果添加的纹理贴图过多,会非常影响游戏性能,特别是在移 ...
- 第八届蓝桥杯省赛 K倍区间
问题描述 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍区间. ...
- Linux下的串口调试工具——Xgcom
Linux下的串口调试工具——Xgcom xgcom的下载网址:https://code.google.com/archive/p/xgcom/downloads (1)安装必须的库 apt-get ...
- Oracle 12c RAC
前提环境(ip,内核参数,环境变量,存储等等...)已准备好. (一) 安装grid 1. Grid用户登录上传并解压 上传linuxx64_12201_grid_home.zip至/u01/ ...
- Kafka学习之二 Kafka安装和使用
部署环境Linux(Centos 6.5),JDK 1.8.0,zookeeper-3.4.12,kafka_2.11-2.0.0. 1. 单机环境 官方建议使用JDK 1.8版本,因此本文使 ...