c++用for循环怎样实现全排列?请给我个最简单的算法,谢谢!

例如:
int num_index[24][4] = {
{ 0, 1, 2, 3 }, { 0, 1, 3, 2 }, { 0, 2, 1, 3 }, { 0, 2, 3, 1 }, { 0, 3, 1, 2 }, { 0, 3, 2, 1 },
{ 1, 0, 2, 3 }, { 1, 0, 3, 2 }, { 1, 2, 0, 3 }, { 1, 2, 3, 0 }, { 1, 3, 0, 2 }, { 1, 3, 2, 0 },
{ 2, 0, 1, 3 }, { 2, 0, 3, 1 }, { 2, 1, 0, 3 }, { 2, 1, 3, 0 }, { 2, 3, 0, 1 }, { 2, 3, 1, 0 },
{ 3, 0, 1, 2 }, { 3, 0, 2, 1 }, { 3, 1, 0, 2 }, { 3, 1, 2, 0 }, { 3, 2, 0, 1 }, { 3, 2, 1, 0 }
};
怎样用for循环实现?

 #include <iostream> 
using namespace std;
int n = 0;

void swap(char *a ,char *b)
{
    int m ;
    m = *a;
    *a = *b;
    *b = m;
} 

void perm(char list[],int k, int m )
{
    int i;
    if(k >m)
    {
        for(i = 0 ; i <= m ; i++)
        {
            cout<<"r"<<list[i];

        }
        cout<<"/n";
        n++;
    }
    else
    {
        for(i = k ; i <=m;i++)
        {
            swap(&list[k],&list[i]);
            perm(list,k+1,m);
            swap(&list[k],&list[i]);
        }
    }
}

int main()
{
    char list[] ="12345";
    perm(list,0,4);
    cout<<"total:"<<n<<"/n";     
    return 0;
}

 // ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
using namespace std;

int p(int n)
{
    int r = 1;
    for (int i = 2; i <= n; i++)
        r *= i;
    return r;
}

int main()
{
    int n = 3;
    int c = p(n);
    int ** result = new int *[c];

    for (int i = 0; i < c; i++)
    {
        result[i] = new int[n];
    }
    result[0][0] = 0;

    for (int m = 2; m <= n; m++)
    {
        for (int i = 1; i < m; i++)
        {
            for (int j = 0; j < p(m - 1); j++)
            {
                for (int k = 0; k < m; k++)
                {
                    result[i * p(m - 1) + j][k] = result[j][k];
                }
            }
        }
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < p(m - 1); j++)
            {
                for (int k = m ; k > 0; k--)
                {
                    result[i * p(m - 1) + j][k] = result[i * p(m - 1) + j][k < i ? k : k - 1];
                }
                result[i * p(m - 1) + j][i] = m - 1;
            }
        }
    }
    for (int i = 0; i < c; i++)
    {
        for (int j = 0; j < n; j++)
            cout << result[i][j];
        cout << endl;
    }
}

210
201
120
021
102
012
Press any key to continue . . .

这是n=4

 3210
3201
3120
3021
3102
3012
2310
2301
1320
0321
1302
0312
2130
2031
1230
0231
1032
0132
2103
2013
1203
0213
1023
0123
Press any key to continue . . .

说一下思路,初始情况,一个元素全排列
0

第一次复制2份(对于一个数字,有两个插入点,两个数字有三个,以此类推)
0
0
插入
10
01

第二次复制3份
10
01
10
01
10
01

插入
210
201
120
021
102
012

再复制4份
210
201
120
021
102
012
210
201
120
021
102
012
210
201
120
021
102
012
210
201
120
021
102
012

再插入
3210
3201
3120
3021
3102
3012
2310
2301
1320
0321
1302
0312
2130
2031
1230
0231
1032
0132
2103
2013
1203
0213
1023
0123

得到结果

方法一 递归:
#include
using namespace std;
void Permutation(char *str,int start,int end)
{
if(start==end)
{
cout< }
else
{
for(int i=start;i {
swap(str[i],str[start]);
Permutation(str,start+1,end);
swap(str[i],str[start]);
}
}
}
int main()
{
char str[]="123";
Permutation(str,0,2);
return 0;
}
方法二 STL:
#include

using namespace std;

int main()

{

char str[]="1423";

sort(str,str+strlen(str));

cout< while(next_permutation(str,str+strlen(str)))
{
cout }
return 0;
}
方法三 dfs:
#include
#define MAX_SIZE 10
int n;//要进行排列的大小
int array[MAX_SIZE];//记录排列的顺序
int sign[MAX_SIZE];//记录某数是否已经排列过
int count;
void DFS(int step)//深度优先搜索step从0开始
{
if(step==n)//已经结束输出结果
{
count++;
for(int i=0;i<n;i++)
printf("%d ",array[i]);
printf("\n");
return;
}
for(int i=1;i<=n;i++)
{
if(sign[i-1]==0)//假如还没有排列
{
sign[i-1]=1;//设为1表示已经排列
array[step]=i;//排列的数值
DFS(step+1);//排列下一个数
sign[i-1]=0;//回到前一步
}
}
return;
}
int main()
{
while(true)
{
printf("请输入n的值:\n");
scanf("%d",&n);
DFS(0);
printf("总共有%d种情况\n",count);
count=0;
}
return 0;
}

int arry[10];
myFun(4, 0, arry);

void myFun(const int c, int n, int arry[])
{
int *tm = new int[n + 1];
if (c == n)
return ;
if (n < 0)
return;
//add a element
arry[n] = n + 1;
n = n + 1;
//bei fen arry
for (int k = 0; k < n; k++)
tm[k] = arry[k];

if (n == 1 && c == 1)
{
    printf("%d\n", arry[n - 1]);
    return;
}

for (int i = n - 1; i >= 0; i--)
{
    //huan yuan arry
    for (int k = 0; k < n; k++)
        arry[k] = tm[k];

    myFun(c, n, arry);
    if (n == c)
    {
        for (int j = 0; j < c; j++)
            printf("%d ", arry[j]);
        printf("\n");
    }
    if (i > 0)
    {
        int temp = tm[i];
        tm[i] = tm[i - 1];
        tm[i - 1] = temp;
    }
}

}