帮帮蒟蒻吧~
查看原帖
帮帮蒟蒻吧~
1031089
25tcx楼主2023/11/25 13:41

代码:

#include <bits/stdc++.h>
using namespace std;
//string a[100005];
void dfs(int x,int n,vector<int>& box,vector<int>& visited){
	int c=sizeof(box);
	if(x==n+1){
		for(int i=1;i<c;i++){
			cout<<box[i]<<" ";
		}
		cout<<"\n";
		return;
	}
	for(int i=1;i<=n;i++){
		if(visited[i]==0){
			box[x]=i;
			visited[i]=1;
			dfs(x+1,n,box,visited);
			visited[i]=0;
		}
	}
}
int main()
{
//	freopen("name1.in","r",stdin);
//	freopen("name1.out","w",stdout);
	int n;
	vector<int> box;
	vector<int> fhj;
	cin>>n;
	box.resize(n+1,0);
	fhj.resize(n+1,0);
	dfs(1,n,box,fhj);
	return 0;
}

输出:(样例输入:3)小熊猫Dev-C++自测

3
1 2 3 -2061570609 28369 12987128 12976320 -1038094967 201354960 0 -1
1 3 2 -2061570609 28369 12987128 12976320 -1038094967 201354960 0 -1
2 1 3 -2061570609 28369 12987128 12976320 -1038094967 201354960 0 -1
2 3 1 -2061570609 28369 12987128 12976320 -1038094967 201354960 0 -1
3 1 2 -2061570609 28369 12987128 12976320 -1038094967 201354960 0 -1
3 2 1 -2061570609 28369 12987128 12976320 -1038094967 201354960 0 -1

--------------------------------
Process exited after 0.9945 seconds with return value 0
请按任意键继续. . .

大佬帮帮蒟蒻吧~

2023/11/25 13:41
加载中...