码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-24 09:56:17      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:for   scan   回溯   strong   use   style   names   github   代码   

题目链接: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
****************************************************************/

 

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

标签:for   scan   回溯   strong   use   style   names   github   代码   

原文地址:http://www.cnblogs.com/zpfbuaa/p/6755032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!