首页 > 其他分享 >NC15128 老子的全排列呢

NC15128 老子的全排列呢

时间:2022-09-04 08:44:07浏览次数:87  
标签:排列 题目 int DFS 老子 STL NC15128

题目

  • 原题地址:老子的全排列呢
  • 题目编号:NC15128
  • 题目类型:DFS
  • 时间限制:C/C++ 1秒,其他语言2秒
  • 空间限制:C/C++ 32768K,其他语言65536K

1.题目大意

  • 输出18的全排列

2.题目分析

  • \(\rm DFS\) 也可以做,但是发现 \(\rm STL\) 是真的好用:
  • next_permutation(s.begin(), s.end()):让一个排列变成全排列的下一项
  • pre_permutation(s.begin(), s.end()):让一个排列变成全排列的上一项
  • 能换就返回true,不能换就返回false

3.题目代码

①STL做法

#include <bits/stdc++.h>

using namespace std;

int main() {
    string s = "12345678";
    do {
        for(auto k:s) cout << k << ' ';
            cout << endl;
    }while(next_permutation(s.begin(), s.end()));
}

①DFS做法

#include <bits/stdc++.h>

using namespace std;

int a[8], b[8];
void dfs(int x) {
    if(x&8){for(auto k:a) cout << k << ' ';cout << endl;}
    else for(int i=0;i<8;i++) {if(!b[i])b[i]=1,a[x]=i+1,dfs(x+1),b[i]=0;}
}

int main() {
    dfs(0);
}

标签:排列,题目,int,DFS,老子,STL,NC15128
From: https://www.cnblogs.com/zhangyi101/p/16654212.html

相关文章