链接: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
来源:牛客网
时间限制: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;
}