【codevs 1294】全排列
2016-07-21 22:51
260 查看
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
想象一下,有n个可以放数的空位
每放一个就继续放下一个直到没有空位,回溯
记录每次放满
即为全排列⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄
以上
时间限制: 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
想象一下,有n个可以放数的空位
每放一个就继续放下一个直到没有空位,回溯
记录每次放满
即为全排列⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int n,ans[15]; bool b[15]; //x表示是否还有空位,当且仅当x>0时,继续放 void qpl(int x){ if(x > 0){ for(int i = 1; i <= n; i++) if(b[i]!=0) continue; //如果这一位上没有放数 ans[x]=i;//放上数 b[i]=1;//记录一下放上了数 qpl(x-1);//继续放数 b[i]=0;//放完数了,清空状态 } else for(int i = n; i >=1; i--) printf("%d ",ans[i]);//放好了数,输出,即为一种排列 puts(""); } int main(){ scanf("%d",&n); qpl(n); return 0; }
以上
相关文章推荐
- Oracle数据库查询优化技巧
- Oracle数据库查询优化技巧
- 广义线性模型
- LeetCode - 147. Insertion Sort List
- HTTP状态代码以及定义
- An Indexed Cube
- 【codeup 1928】日期差值
- iOS开发从入门到精通-- 单行文本UITextField
- CSS3响应式布局总结
- Android Gson使用笔记
- CentOS7安装gtk+2.0和mysql及环境配置
- Android引用项目B实现项目A支撑需要解决InflateException问题
- Objective-C & Sprite Kit太空历险记 : 1.欢迎加入“OC & SK舰队”
- 递归删除Windows某一文件夹下的 .svn文件夹
- 数组中只出现一次的数
- Git自动换行符
- Android Canvas绘图详解(图文)
- asp.net mvc 提交model 接收不了
- Python之mongodb操作
- net基础