请大佬帮我看看这哪里错了!!!
查看原帖
请大佬帮我看看这哪里错了!!!
759098
tangzirui1016楼主2024/5/22 23:21

代码如下:

#include<bits/stdc++.h>
using namespace std;
int n, a[15], ans;
bool f[15];
string w;
void dfs(int step, string s) {
	if (ans >= n - 1) return;
	if (step > n) {
		ans++;
		if (ans == n - 1) cout << s;
		return;
	}
	for (int i = 1; i <= n; i++) {
		if (f[i]) continue;
		f[i] = 1;
		dfs(step + 1, s + to_string(a[i]) + " ");
		f[i] = 0;
	}
}
int main() {
	//freopen("P2525_2.in","r",stdin);
	//freopen("P2525_2_2.out","w",stdout);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	dfs(1, "");
	if (ans == 0) cout << "ERROR";
	return 0;
}

以下展示测试点数据和我的程序输出的数据。

输入数据:4

3 1 2 4 测试点输出数据:3 2 1 4

我的程序输出的数据:3 2 1 4

??????????????? 谢谢!!!

2024/5/22 23:21
加载中...