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

codevs——1294 全排列

时间:2017-09-11 19:34:31      阅读:366      评论:0      收藏:0      [点我收藏+]

标签:std   sample   一个   wrapper   str   ble   name   dfs   void   

1294 全排列

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

给出一个n, 请输出n的所有全排列

输入描述 Input Description

读入仅一个整数n   (1<=n<=10)

输出描述 Output Description

一共n!行,每行n个用空格隔开的数,表示n的一个全排列。并且按全排列的字典序输出。

样例输入 Sample Input

3

样例输出 Sample Output

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
bool vis[100];
int n,a[100];
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1; ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0; ch=getchar();}
    return x*f;
}
void dfs(int now)
{
    if(now==n+1) 
    {
        for(int i=1;i<=n;i++)
         printf("%d ",a[i]);
        printf("\n");
        return ;
    }
    for(int i=1;i<=n;i++)
     if(!vis[i])
     {
         vis[i]=true;
         a[now]=i;
         dfs(now+1);
         vis[i]=false;
     }
    return ;    
}
int main()
{
    n=read();
    dfs(1);
    return 0;
}

 

codevs——1294 全排列

标签:std   sample   一个   wrapper   str   ble   name   dfs   void   

原文地址:http://www.cnblogs.com/z360/p/7506290.html

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