wikioi 1294 全排列 dfs

 2023-09-05 阅读 19 评论 0

摘要:1294 全排列 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给出一个n, 请输出n的所有全排列 输入描述 Input Description 读入仅一个整数n (1<=n<=10) 输出描述 Output Description 一共n!行,每行n个用空格隔开的数&

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;}elsecout<<" "<<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;
}

 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/1636.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息