生成1~n的排列,以及生成可重集的排列
#include <iostream>
using namespace std;
void printPermutation(int n, int* A, int cur)
{
if (cur == n) { // 递归边界
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
else {
for (int i = 1; i <= n; i++) { // 尝试在A[cur]中填各种整数i
int ok = 1;
for (int j = 0; j < cur; j++) {
if (A[j] == i) {
ok = 0; // 如果i已经在A[0]~A[cur-1]出现过,则不能再选
}
}
if (ok) {
A[cur] = i;
printPermutation(n, A, cur + 1); // 递归调用
}
}
}
}
int main()
{
int A[20];
printPermutation(5, A, 0); // 生成1~5的排列
return 0;
}
循环变量 i 是当前考察的A[cur]。为了检查元素i是否已经用过,上面的程序用到了一个标志变量ok,初始值为1(真),如果发现有某个A[j] == i 时,则改为0(假)。如果最终ok仍未1,则说明i没有在序列中出现过,把它添加到序列末尾(A[cur] = i)后递归调用。
声明一个足够大的数组A,然后调用printPermutation(n, A, 0),即可按字典序输出1~n的所有排列。
如果问题变成输入数组p,并按字典序输出数组A个元素的所有全排列,则需要修改代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int P[100], A[100];
// 输出数组p中元素的全排列。数组p中可能有重复元素
void printPermutation(int n, int* P, int* A, int cur) {
if (cur == n) {
for (int i = 0; i < n; i++) printf("%d ", A[i]);
printf("\n");
}
else for (int i = 0; i < n; i++) {
if (!i || P[i] != P[i - 1]) {
int c1 = 0, c2 = 0;
for (int j = 0; j < cur; j++) {
if (A[j] == P[i]) {
c1++;
}
}
for (int j = 0; j < n; j++) {
if (P[i] == P[j]) {
c2++;
}
}
if (c1 < c2) {
A[cur] = P[i];
printPermutation(n, P, A, cur + 1);
}
}
}
}
int main()
{
int i, n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &P[i]);
}
sort(P, P + n);
printPermutation(n, P, A, 0);
return 0;
}
最后用STL中的库函数next_permultation
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int n, p[10];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
sort(p, p + n); // 排序,得到p的最小排列
do {
for (int i = 0; i < n; i++) {
printf("%d ", p[i]); // 输出排列p
}
printf("\n");
} while (next_permutation(p, p + n)); // 求下一个排列
return 0;
}
上述代码同样适用于可重集。
生成1~n的排列,以及生成可重集的排列的更多相关文章
- 生成1~n的排列(模板),生成可重集的排列(对应紫书P184, P185)
生成1~n的排列: #include<iostream> using namespace std; void print_permutation(int n, int *A, int cu ...
- STL next_permutation(a,a+n) 生成一个序列的全排列。满足可重集。
/** 题目: 链接: 题意: 思路: */ #include <iostream> #include <cstdio> #include <vector> #in ...
- 给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合
给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合 ruby代码: def all_possible_arr arr, length = 5 ret = [] leng ...
- [DFS]排列的生成
排列的生成 Time Limit:1000MS Memory Limit:65536K Total Submit:150 Accepted:95 Description 输出P(n,m)的排列(n,m ...
- C# 调用WebService的3种方式 :直接调用、根据wsdl生成webservice的.cs文件及生成dll调用、动态调用
1.直接调用 已知webservice路径,则可以直接 添加服务引用--高级--添加web引用 直接输入webservice URL.这个比较常见也很简单 即有完整的webservice文件目录如下图 ...
- 把验证码和生成时间负值给$_SESSION[vCode]生成图像给浏览器
php 图片 中文验证码 <img src="verify_image.php" alt="点此刷新验证码" name="verify_code ...
- wsdl自动生成Java代码,根据wsdl生成Java代码
wsdl自动生成Java代码,根据wsdl生成Java代码 >>>>>>>>>>>>>>>>>&g ...
- VS2010-使用“预先生成事件命令行”和“后期生成事件命令行”功能
原文:VS2010-使用"预先生成事件命令行"和"后期生成事件命令行"功能 xcopy /r /y $(TargetPath) $(ProjectDir)..\ ...
- 对抗生成网络-图像卷积-mnist数据生成(代码) 1.tf.layers.conv2d(卷积操作) 2.tf.layers.conv2d_transpose(反卷积操作) 3.tf.layers.batch_normalize(归一化操作) 4.tf.maximum(用于lrelu) 5.tf.train_variable(训练中所有参数) 6.np.random.uniform(生成正态数据
1. tf.layers.conv2d(input, filter, kernel_size, stride, padding) # 进行卷积操作 参数说明:input输入数据, filter特征图的 ...
随机推荐
- day07 Cookie 和 Session(非常重要)
day07 Cookie 和 Session 1. 会话技术 2. cookie 的方法和 cookie 案例-显示用户上次访问网站的时间 3. cookie 的细节 - 删除 cookie 4. S ...
- 实践详细篇-Windows下使用VS2015编译的Caffe训练mnist数据集
上一篇记录的是学习caffe前的环境准备以及如何创建好自己需要的caffe版本.这一篇记录的是如何使用编译好的caffe做训练mnist数据集,步骤编号延用上一篇 <实践详细篇-Windows下 ...
- Go 错误处理
Go 语言通过内置的错误接口提供了非常简单的错误处理机制. error类型是一个接口类型,这是它的定义: type error interface { Error() string } 我们可以在编码 ...
- 在查询语句中使用 NOLOCK 和 READPAST
对于非银行等严格要求事务的行业,搜索记录中出现或者不出现某条记录,都是在可容忍范围内,所以碰到死锁,应该首先考虑,我们业务逻辑是否能容忍出现或者不出现某些记录,而不是寻求对双方都加锁条件下如何解锁的问 ...
- Bootstrap3 栅格系统-嵌套列
为了使用内置的栅格系统将内容再次嵌套,可以通过添加一个新的 .row 元素和一系列 .col-sm-* 元素到已经存在的 .col-sm-* 元素内.被嵌套的行(row)所包含的列(column)的个 ...
- Tomcat7源码环境搭建
一.下载Tomcat7源码 从官网上下载Tomcat源码, http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.70/src/apache-t ...
- Android艺术开发探索——第二章:IPC机制(下)
Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...
- Python 继承标准类时发生了什么
定义标准类dict的一个子类c: >>> class c(dict): pass >>> y=c({1:2,3:4}) >>> y {1: 2, ...
- android galley实现画廊效果
青春流水指间. 每段路,都有即将要来的旅程 每颗心,都有值得期待的成分 Android之ImageSwitcher,Gallery用法 今天在做一个软件界面时用到了ImageSwitcher和Gall ...
- androidApp的完全退出
思路:搜集整个工程所有的activity,通过循环把工程中所有的activity都关闭. 搜集工程中的activity,可以由单例模式实现, [java] view plaincopy import ...