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

链接:https://www.nowcoder.com/acm/contest/76/H
来源:牛客网


时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述 
老李见和尚赢了自己的酒,但是自己还舍不得,所以就耍起了赖皮,对和尚说,光武不行,再来点文的,你给我说出来1-8的全排序,我就让你喝,这次绝不耍你,你能帮帮和尚么?
输入描述:

输出描述:
1~8的全排列,按照全排列的顺序输出,每行结尾无空格。
示例1
输入
No_Input
输出
Full arrangement of 1~8
备注:
1~3的全排列  :
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2

3 2 1


题意:输出1~8全排列

思路:1. 用dfs过

            2. 练习一下next_permutation()函数

代码1:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int a[10], vis[10];
void dfs(int cur)
{
	if(cur == 8){
		for(int i = 0; i < 7; i++){
			cout << a[i] << " ";
		}
		cout << a[7] << endl;
		return ;
	}
	for(int i = 0; i < 8; i++){
		if(!vis[i+1]){
			vis[i+1] = 1;
			a[cur] = i+1;
			dfs(cur+1);
			vis[i+1] = 0;
		}
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	for(int i = 0; i < 8; i++){
		a[i] = i+1;
	}
	dfs(0);
    return 0;
}

代码2:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int a[10];
int main()
{
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	for(int i = 0; i < 8; i++){
		a[i] = i+1;
	}
	do{
		for(int i = 0; i < 7; i++){
			cout << a[i] << " ";
		}
		cout << a[7] << endl;
	}while(next_permutation(a, a+8));
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值