设为首页 加入收藏

TOP

回溯法求n的全排列
2017-10-13 10:07:31 】 浏览:8078
Tags:回溯 排列

 

代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <map>
#include <bitset>
using namespace std;
typedef long long LL;
int x[105];
int n;
void Backtrack(int t)
{
    if(t==n){
        for(int i=1;i<=n;i++)
        cout<<x[i]<<" ";
        cout<<endl;
        return ;
    }
    for(int i=t;i<=n;i++)
    {
        swap(x[i],x[t]);
        Backtrack(t+1);
        swap(x[i],x[t]);
    }
}
int main()
{
    for(int i=1;i<105;i++)
        x[i]=i;
    while(scanf("%d",&n)!=EOF)
    {
        cout<<n<<"的全排列如下:"<<endl;
        Backtrack(1);
        cout<<"finish!"<<endl;
    }
    return 0;
}

 

运行截图:

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇回溯法求n的全排列 下一篇Gym 100703I---Endeavor for perf..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目