全排列函数C++实现
例题:求由123456789构成的所有九位数字
1 用C++的next_permutation函数
#include <iostream>
#include <stdio.h>
#include <algorithm>
int main(){
int a[] = {,,,,,,,,};
while(next_permutation(a, a+)){
for(int i =0;i<9;i++)
cout<<a[i];
cout<<endl;
}
return ;
}
注意:
1 要添加头文件#include <algorithm>
2 输出的所有数组,并不包含初始数组,即123456789
2 利用dfs思想实现
#include <iostream>
using namespace std;
int t[];
bool vist[];
void dfs(int start){
if(start == ){
//输出数组
}else{
for(int i=;i<;i++){
if(vist[i] == ){
t[start] = i;
vist[i] = ;
dfs(start+);
vist[i] = ;
}
}
}
}
int main(){
int a[] = {,,,,,,,,};
for(int i=;i<;i++)
vist[i] = ;
dfs();
return ;
}
全排列函数C++实现的更多相关文章
- C++ STL 全排列函数
C++ 全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
- POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快
排列 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21268 Accepted: 8049 Description 题 ...
- next_permutation() 全排列函数
next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...
- C++ 全排列函数 nyoj 366
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- C++中全排列函数next_permutation用法
最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...
- C++ 全排列函数 std::next_permutation与std::prev_permutation
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- C++ STL 全排列函数详解
一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a ...
- STL - next_permutation 全排列函数
学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...
- 全排列函数(next_permutation)
顾名思义,这个函数就是用来求数组的全排列的,至于怎么用,看下面的介绍: 这是一个c++函数,包含在头文件algorithm里面,这个函数可以从当前的数组的大小按照字典序逐个递增的顺序排列 看下面的模板 ...
随机推荐
- 004PHP基础知识——数据类型(一)
<?php /* * 数据类型(一) * 标量类型:整型(int) 浮点型(float) 字符串型(string) 布尔型(boolean) * 复合类型:数组(array) 对象(object ...
- Last_SQL_Errno: 1062
Last_SQL_Errno: 1062: Last_Error: Error 'Duplicate entry '212' for key 'PRIMARY'' on query. Default ...
- Annotation方式实现AOP
1.添加其他jar包 2.配置applicationContext.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version=&quo ...
- eclipse ndk 配置和简单开发demo
记录下以备忘: android开发的各种资源国内镜像 http://www.androiddevtools.cn/ 前端时间尝鲜用android stuido1.5开发了个android的小项目,发现 ...
- C# Http方式下载文件到本地类
直接上代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- reactNative 的一些学习
手把手视频 学习资料大全 入门系列
- 浅析C#中ASP.NET页面的生存周期
一般来说,页要经历下表概述的各个阶段.除了页生命周期阶段以外,在请求前后还存在应用程序阶段,但是这些阶段并不特定于页. 阶段 说明 页请求 页请求发生在页生命周期开始之前.用户请求页时,ASP.NET ...
- 自己WIN7旗舰版安装 SQLServer2005/2008的一些总结
准备工作:下载安装包,当然要保证安装包能用: 安装:1.设置setup.exe文件 右键属性选择 --兼容,兼容下面选择---以管理员方式运行,---兼容模式选择windows xp或者windows ...
- boost库之udp广播实例
//UdpLinkServer.h //udp服务 #pragma once #include <boost/asio/ip/tcp.hpp> #include <boost/asi ...
- Win10 64bit下安装GPU版Tensorflow+Keras
Tensorflow和Keras都是支持Python接口的,所以本文中说的都是搭建一个Python的深度学习环境. Keras是对Tensorflow或者Theano的再次封装,也就是以Tensorf ...