zoukankan      html  css  js  c++  java
  • 全排列函数C++实现

    例题:求由123456789构成的所有九位数字

    1 用C++的next_permutation函数

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    int main(){
        int a[9] = {1,2,3,4,5,6,7,8,9}; 
            while(next_permutation(a, a+9)){
                for(int i =0;i<9;i++)
                    cout<<a[i];
                cout<<endl;
            }
        return 0;
    }    

    注意:

      1 要添加头文件#include <algorithm>

      2 输出的所有数组,并不包含初始数组,即123456789

    2 利用dfs思想实现

    #include <iostream>
    using namespace std;
    int t[10];
    bool vist[10];
    void dfs(int start){ if(start == 10){
          //输出数组 }
    else{ for(int i=1;i<10;i++){ if(vist[i] == 0){ t[start] = i; vist[i] = 1; dfs(start+1); vist[i] = 0; } } } } int main(){ int a[9] = {1,2,3,4,5,6,7,8,9}; for(int i=1;i<10;i++) vist[i] = 0; dfs(1);
      return 0; }
  • 相关阅读:
    软考
    十步走-阅读笔记
    软著申请
    十步走-阅读笔记
    基于Ubuntu安装部署ZooKeeper
    基于Ubuntu安装JDK(OPenJDK8)
    Intern Day89
    阿里巴巴Java研发工程师技术一面
    面试
    6_moc.md
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/10320277.html
Copyright © 2011-2022 走看看