1. 程式人生 > >全排列演算法的實現(糾正“啊哈”演算法書籍中的錯誤)

全排列演算法的實現(糾正“啊哈”演算法書籍中的錯誤)

本程式在VS2013下除錯通過,若有疑問可以評論大家探討

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

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
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; }