2018年全国多校算法寒假训练营练习比赛(第四场) H - 老子的全排列呢(next_permutation)

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Tony5t4rk/article/details/79392355

Desctiption:

老李见和尚赢了自己的酒,但是自己还舍不得,所以就耍起了赖皮,对和尚说,光武不行,再来点文的,你给我说出来1-8的全排序,我就让你喝,这次绝不耍你,你能帮帮和尚么?

Input:

Output:

1~8的全排列,按照全排列的顺序输出,每行结尾无空格。

Sample Input:

No_Input

Sample Output:

Full arrangement of 1~8

Hint:

1~3的全排列 :
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

题目链接

这道题用STL里的next_permutation很好写,解释博文

next_permutation(全排列算法)

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <set>
#include <map>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 1e+5;
const double eps = 1e-5;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;

int num[8] = {1, 2, 3, 4, 5, 6, 7, 8};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    do {
        cout << num[0] << " " << num[1] << " " << num[2] << " " << num[3] << " " << num[4] << " " << num[5] << " " << num[6] << " " << num[7] << endl;
    } while(next_permutation(num, num + 8));
    return 0;
}

展开阅读全文

没有更多推荐了,返回首页