题解 | #老子的全排列呢#

老子的全排列呢

https://ac.nowcoder.com/acm/problem/15128

方法一:直接用stl里面的next_permutation

```// #include<bits/stdc++.h>
// using namespace std;
// #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// int main(){
//     IOS;
//     int a[8]={1,2,3,4,5,6,7,8};
//     do{
//         for(int i=0;i<8;i++){
//             cout<<a[i]<<' ';
//         }
//         cout<<endl;
//     }while(
//         next_permutation(a,a+8);
//     );
//     return 0;
// }


> 方法二:用dfs的
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int a[8];  
int vis[8];//记录是否用过
void def(int x){
	if(x>8){
		for(int i=1;i<=8;i++){
			cout<<a[i]<<" ";
		}
		cout<<endl;
		return;
	}
	for(int i=1;i<=8;i++){
		if(vis[i]==1){
			continue;
		}
		a[x]=i;
		vis[i]=1;
		def(x+1);
		a[x]=0;
		vis[i]=0;
	}
	
}
int main(){
	IOS;
	def(1);
	return 0;
} 
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 13:54
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:20
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务