1到8的全排列

#include <iostream>
#include <cstring>
#include <string>
#include<algorithm>
using namespace std;
int v[10];
int a[10];
void display()
{
    int i;
    for (i = 1; i <= 8; i++)
    {
        if (i == 8) cout << a[i] << endl;
        else cout << a[i] << " ";
    }
}
void dfs(int s)
{
    int i;
    if (s == 9) display();
    for (i = 1; i <= 8; i++)
    {
        if (v[i]==0)
        {
            v[i] = 1;
            a[s] = i;
            dfs(s+1);
            v[i] = 0;
        }
    }
}
int main()
{
    memset(v, 0, sizeof(v));
    dfs(1);
    return 0;
}

 

posted on 2018-02-11 14:41  蔡军帅  阅读(794)  评论(0编辑  收藏  举报