求1,2,3,4,5,6的全排列。即将这6个数以任意不同顺序排列,将所有可能得到的6位数都列出来。

来源:百度知道 编辑:UC知道 时间:2024/09/23 13:28:49
请用C++表示

就是6的全排列,P(6)6=6*5*4*3*2*1

============================================
code
============================================
#include <iostream>
using namespace std;

void perm(int p, int n, int *res);

int main()
{
// get the permutation of [n]:={1,2,...,n} with n integer 1~9
int n = 0;
int res[9] = {};

cout << "input an integer between 1 ~ 9: ";
cin >> n;
cout << endl;

perm(0, n, res);

cout << endl;
system("pause");

return 0;
}

void perm(int p, int n, int *res)
{
bool judge;

if (p < n)
{
for (int i = 1; i < n+1; i++)
{
judge = false;

for (int j = 0; j < p; j++)
{
if (i == res[j])
{
judge = true;
break;
}
}

if (judge ==