邹平做网站的联系方式,建设公司的网站首页,菜单制作软件app,辽宁省建设工程信息网盲盒系统把 1∼n 这 n 个整数排成一行后随机打乱顺序#xff0c;输出所有可能的次序。
输入格式 一个整数 n。
输出格式 按照从小到大的顺序输出所有方案#xff0c;每行 1 个。
首先#xff0c;同一行相邻两个数用一个空格隔开。
其次#xff0c;对于两个不同的行#xff0c;…把 1∼n 这 n 个整数排成一行后随机打乱顺序输出所有可能的次序。
输入格式 一个整数 n。
输出格式 按照从小到大的顺序输出所有方案每行 1 个。
首先同一行相邻两个数用一个空格隔开。
其次对于两个不同的行对应下标的数一一比较字典序较小的排在前面。
数据范围 1≤n≤9 输入样例 3 输出样例 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
代码如下
#include iostream
using namespace std;
const int N 1010;
int a[N];
bool st[N];
int n;void dfs(int u) {if (u n) {for (int i 1; i n; i) {cout a[i] ;}cout endl;return ;}for (int i 1; i n; i) {if (!st[i]) {st[i] true;a[u] i;dfs(u 1);st[i] false;a[u] 0;}}
}int main() {cin n;dfs(1);return 0;
}