19-擅长排列的小明

内存限制:64MB
时间限制:1000ms
Special Judge: No

accepted:10
submit:16

题目描述:

小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。

输入描述:

第一行输入整数N(1<N<10)表示多少组测试数据,
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)

输出描述:

在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例

样例输入:

复制

2
3 1
4 2

样例输出:

1
2
3
12
13
14
21
23
24
31
32
34
41
42
43 分析:
  根据全排列的性质,一组包含所有组合的全排列(ps:假设该组数由n个元素组成,我们要从中选择m个元素,问它的组合情况),
  它所对应的前m个元素,就是它的所有选择、组合情况 扩展:
  equal(A, A+m, B);
  //用于比较数组A和数组B是否相同,即就是取数组A的前m个元素与数组B做比较 步骤:
  ①、初始化A为1到n组成的数组
  ②、通过STL中的next_permutation()创建数组A的全排列
  ③、如果下一个排列与上一个排列不同就用temp来存放下一个排列 核心代码:
 do
{
if(!equal(A, A+m, temp))
{
for(int i = ; i < m; ++ i)
{
temp[i] = A[i];
printf("%d", temp[i]);
}
printf("\n");
}
} while(next_permutation(A, A+n));

C/C++代码实现(AC):

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <stack>
#include <map>
#include <queue> using namespace std;
const int MAXN = ;
int A[MAXN]; void cal_array(int n)
{
for(int i = ; i < n; ++ i)
A[i] = i + ;
} int main()
{
int t;
scanf("%d", &t);
while(t --)
{
int n, m, temp[MAXN] = {};
scanf("%d%d", &n, &m);
cal_array(n); do
{
if(!equal(A, A + m, temp)) // 如果数组A的前m个元素与temp两个数组不想等
{
for(int i = ; i < m; ++ i)
{
temp[i] = A[i];
printf("%d", temp[i]);
}
printf("\n");
}
} while(next_permutation(A, A + n));
}
return ;
}

最新文章

  1. [DOM Event Learning] Section 4 事件分发和DOM事件流
  2. hibernate关联关系笔记
  3. Mysql的二进制日志binlog的模式说明
  4. hibernate query.list() 返回的数据类型
  5. HDU 1102 最小生成树裸题,kruskal,prim
  6. 【Android开发】 第一课 环境搭建教程
  7. Object-C Categories和Protocols
  8. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [4] 首页 APP 接口开发方案 ③ 定时读取缓存方式
  9. android 常用命令
  10. 多次读取请求request里数据
  11. ms08-067
  12. POJ 3007 Organize Your Train part II
  13. 【charger battery 充電 充電器 電池】過充保護警告訊息 over charging protection,Battery over voltage protection, warning message
  14. Ubuntu下Tomcat初始配置
  15. gitlab与jenkins的自动化部署(通过webhook与ansilble)
  16. weakhashmap简单理解
  17. 科学计算和可视化(numpy及matplotlib学习笔记)
  18. php+Ajax 例子
  19. js/jquery遇到的坑总结
  20. 索引Log

热门文章

  1. boost::thread_specific_ptr
  2. Thief-Book 上班摸鱼神器
  3. shell变量(二)
  4. NOMP矿池搭建
  5. 再不努力提高效率,小姐姐都被人追走了:K8S一键部署了解一下?
  6. .NET Framework概述
  7. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
  8. Java_条件控制与循环控制
  9. Mysql数据库(六)视图
  10. 微信小程序的canvas和遮盖布颜色设置问题