题目1120:全排列(回溯法)

题目链接:http://ac.jobdu.com/problem.php?pid=1120

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

参考代码:

//
//  1120 全排列.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 23/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define MAX_SIZE 7
 
using namespace std;
 
char str[MAX_SIZE];
char ans[MAX_SIZE];
bool used[MAX_SIZE];
int len;
 
void prem(int x){
    if(x == len){
        ans[x]='';
        printf("%s
",ans);
        return;
    }
    for(int i = 0 ; i < len ; i++){
        if(!used[i]){
            used[i] = true;
            ans[x] = str[i];
            prem(x+1);
            used[i]=false;
        }
    }
}
int main(){
    while(scanf("%s",str)!=EOF){
        memset(used,false,sizeof(used));
        len = (int)strlen(str);
        prem(0);
        printf("
");
    }
    return 0;
}
/**************************************************************
    Problem: 1120
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:440 ms
    Memory:1520 kb
****************************************************************/
原文地址:https://www.cnblogs.com/zpfbuaa/p/6755032.html