x位全排列(next_permutation)
擅长排列的小明
- 描述
- 小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。
- 输入
- 第一行输入整数N(1<N<10)表示多少组测试数据,
每组测试数据第一行两个整数 n m (1<n<9,0<m<=n) - 输出
- 在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例
- 样例输入
-
- 2
- 3 1
- 4 2
- 2
- 样例输出
-
- 1
- 2
- 3
- 12
- 13
- 14
- 21
- 23
- 24
- 31
- 32
- 34
- 41
- 42
- 43
给定1到n的数,求x位数的全排列
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int main()
- {
- int n,k,num,i;
- char a[],b[];
- cin>>n;
- while(n--)
- {
- cin>>k>>num;
- for(i=;i<k;i++)
- a[i]=''+i+;
- a[k]='\0';
- for(i=;i<num;i++)
- cout<<a[i];
- cout<<endl;
- strcpy(b,a);
- while(next_permutation(a,a+k))
- {
- int flag=;
- for(i=;i<num;i++)
- {
- if(a[i]!=b[i])
- {
- flag=;
- }
- }
- if(flag)
- {
- for(i=;i<num;i++)
- cout<<a[i];
- cout<<endl;
- strcpy(b,a);
- }
- else
- continue;
- }
- }
- }
- 1
x位全排列(next_permutation)的更多相关文章
- 全排列next_permutation()用法和构造函数赋值
全排列next_permutation()用法 在头文件aglorithm里 就是1~n数组的现在的字典序到最大的字典序的依次增加.(最多可以是n!种情况) int a[n]; do{ }while( ...
- 全排列 next_permutation 用法
给一个正整数n,让你求它的全排列 先介绍一个函数,iota(a,a+n,1) 用法就是把a数组的第0位到第n-1位依次赋为1,2,.....n: 然后是next_permutation(a,a+4)函 ...
- 排列2(全排列next_permutation 注意格式)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- STL中关于全排列next_permutation以及prev_permutation的用法
这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
- 全排列 ( next_permutation)
SLT: C++的STL有一个函数可以方便地生成全排列,这就是next_permutation 在C++ Reference中查看了一下next_permutation的函数声明: #include ...
- 蓝桥杯 方格填数 DFS 全排列 next_permutation用法
如下的10个格子(参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 请填写表示方案数目的整数.注意:你提交的应该是一个 ...
- 全排列 next_permutation() 函数的用法
在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: #incl ...
- 全排列 next_permutation() 函数的使用
看来看去还是这篇博客比较简洁明了 https://www.cnblogs.com/My-Sunshine/p/4985366.html 顺便给出牛客网的一道题,虽然这道题用dfs写出全排列也能做,题意 ...
随机推荐
- mvc4 基于Area实现插件模块化开发
对于一个较大规模的Web应用,可以从功能上通过Area将其划分为为较小的单元.每个Area相当于一个独立的子系统,具有一套包含Model.Views和Controller在内 的目录结构和配置文件.一 ...
- jquery checkbox 全选、取消全选
$("#checkall").click(function(){ $("input[name='uid']").prop("checked" ...
- UltraChart导出图片
? //一定要先绑定UltraChart,如果先绑定,然后有点击图片导出,没有用的 string fulPath="xxxx"; this.UltraChartTScore.Sav ...
- Dynamics CRM 2013 初体验(2):UI
Dynamics CRM 2013 系统的UI与2011相比改动是巨大的:传统的导航栏被去掉了,取代它的是win8风格的小磁铁:Ribbon风格的工具栏也被去掉啦,它的风格将回滚至4.0时代:新系统添 ...
- if switch练习(体重)
public class shencai { public static void main(String[] args) { int h= 186,g= 80; String Sex = " ...
- Ubuntu 配置Tomcat环境
1.下载Tomcat http://tomcat.apache.org/,下载Tomcat 8(由于目前最新eclipse不支持tomcat 9) 将下载的apache-tomcat-8.0.35.t ...
- for添加用户
#!/bin/bash #接受一个参数: #add: 添加用户user1..user10 #del: 删除用户user1..user10 #其它:退出 #定义变量 ADD=add DEL=del #判 ...
- jquery 鼠标图片经过效果
<script> //鼠标图片经过效果 $(function(){ $(".jione_box ").css("background-color", ...
- qt反走样(简选)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #qt反走样(简选) #概念 """ ...
- UIImage图片处理
#pragma mark - #pragma mark - 缩放处理 + (UIImage *)scaleImage:(UIImage *)image withScale:(float)scale { ...