全排列算法的实现(纠正“啊哈”算法书籍中的错误)

本程序在VS2013下调试通过,若有疑问可以评论大家探讨

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

#include "stdafx.h"
#include
#include
using namespace std;
int a[10], book[10], n;

void  dfs(int step){
    int i;
    if (step == n + 1){
        for (i = 1; i <= n; i++){

            printf_s("%d",a[i]);


        }
        printf_s("\n");
    }

    for (i = 1; i <= n; i++){
        if (book[i] == 0)
        {
            a[step] = i;
            book[i] = 1;
            dfs(step + 1);
            book[i] = 0;
        }

    }


}


int _tmain(int argc, _TCHAR* argv[])
{
    scanf_s("%d",&n);
    dfs(1);
    system("pause");
    return 0;
}

你可能感兴趣的:(算法,全排列,算法)