例题:求由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++实现的更多相关文章

  1. C++ STL 全排列函数

    C++  全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...

  2. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  3. POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21268   Accepted: 8049 Description 题 ...

  4. next_permutation() 全排列函数

    next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...

  5. C++ 全排列函数 nyoj 366

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  6. C++中全排列函数next_permutation用法

    最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...

  7. C++ 全排列函数 std::next_permutation与std::prev_permutation

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  8. C++ STL 全排列函数详解

    一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a ...

  9. STL - next_permutation 全排列函数

    学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...

  10. 全排列函数(next_permutation)

    顾名思义,这个函数就是用来求数组的全排列的,至于怎么用,看下面的介绍: 这是一个c++函数,包含在头文件algorithm里面,这个函数可以从当前的数组的大小按照字典序逐个递增的顺序排列 看下面的模板 ...

随机推荐

  1. 【zzuli-2259】matrix

    题目描述 在麦克雷的面前有N个数,以及一个R*C的矩阵.现在他的任务是从N个数中取出 R*C 个,并填入这个矩阵中.矩阵每一行的法值为本行最大值与最小值的差,而整个矩阵的法值为每一行的法值的最大值.现 ...

  2. poj2195

    题解: 简单KM 把每一个男的和房子分离 代码: #include<cstdio> #include<cmath> #include<algorithm> #inc ...

  3. Systems

    package com.System; public class Study01 { /* * System 包含一些游泳的类字段和方法 * 继承自java.lang包 * JDK1.0开始 * 全部 ...

  4. SpringInAction--XML配置Spring Aop

    前面学习了如何用注解的方式去配置Spring aop,今天把XML配置的方法也看了下,下面顺便也做了个记录 先把spring中用xml配置aop的配置元素给贴出来: <aop:advisor&g ...

  5. New Concept English there (6)

    30w/m The expensive shops in a famous arcade near Piccadilly were just opening. At this time of the  ...

  6. 基于AMBA总线的SPI协议IP核的设计与验证

    https://wenku.baidu.com/view/9542213131126edb6f1a1048.html?mark_pay_doc=2&mark_rec_page=1&ma ...

  7. RTP协议学习

    RTP协议是承载在UDP协议之上的应用协议 http://blog.csdn.net/chen495810242/article/details/39207305 http://blog.51cto. ...

  8. Java多线程编程实战指南(核心篇)读书笔记(一)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76422930冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  9. Unity 异步网络方案 IOCP Socket + ThreadSafe Queue

    Unity IOCP Socket + ThreadSafe Queue 1.Socket.BeginReceive系列接口在unityweb下是不正常的,页面刷新会导致问题 2.自己维护线程,会带来 ...

  10. E. Holes(分块)

    题目链接: E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input ...