Fork me on GitHub

题目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]='\0';
        printf("%s\n",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("\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1120
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:440 ms
    Memory:1520 kb
****************************************************************/

 

posted @ 2017-04-24 07:57  伊甸一点  阅读(243)  评论(0编辑  收藏  举报