
Lv.1
回来了(?)
签名:搞一些奇奇怪怪的玩意
在 【编程知识】盘点世界上17常见的编程语言 中回复
既然楼上都在秀,那我也秀个C++,一个全排列,算不得什么
#include <bits/stdc++.h>
using namespace std;
int n;
bool vis[50];
int a[50];
void dfs(int step) {
if (step == n + 1) {
for (int i = 1; i <= n; i++) {
cout << setw(5) << a[i];
}
cout << endl;
return;
}
for (int i = 1; i <= n; i++) {
if (vis[i] == 0) {
vis[i] = 1;
a[step] = i;
dfs(step + 1);
vis[i] = 0;
}
}
return;
}
int main() {
cin >> n;
dfs(1);
return 0;
}2022-09-04T00:13:43 点赞:0