zoukankan      html  css  js  c++  java
  • wikioi 1294 全排列 dfs

    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

    数据范围及提示 Data Size & Hint
     
    //记得把下面的cout全部改成printf 
    //不然会t的
    //我是太懒了,就没改了
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    const int maxn=20;
    int a[maxn];
    int b[maxn];
    int n;
    void init()
    {
        cin>>n;
        return;
    }
    int put()
    {
        int first=1;
        for(int i=1;i<=n;i++)
        {
            if(first)
            {
                printf("%d",a[i]);
                first=0;
            }
            else
                cout<<" "<<a[i];
        }
        cout<<endl;
    }
    void work(int t)
    {
        if(t>n)
        {
            put();
            return;
        }
        for(int i=1;i<=n;i++)
        {
            if(!b[i])
            {
                b[i]=1;
                a[t]=i;
                work(t+1);
                b[i]=0;
            }
        }
    
    }
    int main()
    {
        //sspeed;
        init();
        work(1);
        return 0;
    }
  • 相关阅读:
    vba中数据类型
    Excel统计函数COUNTIF()的常规用法介绍
    分类求和
    在每个sheet的相同位置写入相同的值
    Timer函数
    数组总结
    如何制作本地yum源
    HDFS的优缺点
    HDFS安全模式
    HDFS的HA(高可用)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4135649.html
Copyright © 2011-2022 走看看