#include <bits/stdc++.h>
using namespace std;
int p[20];
int main()
{
int n;
cin >> n;
//枚举 1~n 的所有排列
for (int i = 1; i <= n; i++)
p[i] = i;
do
{
//循环中依次得到了每个排列
//{
for (int i = 1; i <= n; i++)
cout << p[i] << " ";
cout << "\n";
//}
} while (next_permutation(p + 1, p + n + 1));
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n;
int p[20];
bool vis[20];
void dfs(int now)
{
if (now > n)
{
// 这里会依次得到每个排列
//{
for (int i = 1; i <= n; i++)
cout << p[i] << " ";
cout << "\n";
//}
return;
}
for (int i = 1; i <= n; i++)
{
if (vis[i])
continue;
p[now] = i;
vis[i] = true;
dfs(now + 1);
vis[i] = false;
}
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n;
int p[20];
void dfs(int now)
{
if (now > n)
{
//这里会依次得到所有情况(p[1] ~ p[n] 都可以是 0/1)
//{
for (int i = 1; i <= n; i++)
cout << p[i] << " ";
cout << "\n";
//}
return;
}
p[now] = 1;
dfs(now + 1);
p[now] = 0;
dfs(now + 1);
}
int main()
{
cin >> n;
dfs(1);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n;
int p[20];
int main()
{
cin >> n;
for (int i = 0; i <= (1 << n) - 1; i++)
{
// 这里会依次得到所有情况(p[1] ~ p[n] 都可以是 0/1)
//{
for (int j = 1; j <= n; j++)
{
if ((1 << (j - 1)) & i)
p[j] = 1;
else
p[j] = 0;
}
for (int i = 1; i <= n; i++)
cout << p[i] << " ";
cout<<"\n";
//}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int n, k;
int p[20];
void dfs(int now)
{
if (now > n)
{
//这里会依次得到所有情况(p[1] ~ p[n] 都可以是 1~k)
//{
for (int i = 1; i <= n; i++)
cout << p[i] << " ";
cout << "\n";
//}
return;
}
for (int i = 1; i <= k; i++)
{
p[now] = i;
dfs(now + 1);
}
}
int main()
{
cin >> n >> k;
dfs(1);
return 0;
}