第m个全排列
#include<stdio.h>
#include<string.h> int flag,n,m;
int a[],sum,vis[]; void dfs(int k)
{
if(flag==) return;
if(k==n+)
{
sum++;
if(sum==m)
{
flag=;
for(int i=;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
return ;
}
}
else
{
for(int i=;i<=n;i++)
{
if(!vis[i])
{
vis[i]=;
a[k]=i;
dfs(k+);
vis[i]=;
}
}
}
} int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(vis,,sizeof(vis));
sum=;
flag=;
dfs();
}
return ;
}
第m个全排列的更多相关文章
- PHP实现全排列(递归算法)
算法描述:如果用P表示n个元素的全排列,而Pi表示n个元素中不包含元素i的全排列,(i)Pi表示在排列Pi前面加上前缀i的排列,那么n个元素的全排列可递归定义为: ① 如果n=1,则排列P只有一 ...
- hdu5651 xiaoxin juju needs help (多重集的全排列+逆元)
xiaoxin juju needs help 题意:给你一个字符串,求打乱字符后,有多少种回文串. (题于文末) 知识点: n个元素,其中a1,a2,··· ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 全排列算法的JS实现
问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的 ...
- java实现全排列
前天上午的面试遇到了一个用java实现一串数字的全排列的题,想来想去用递归最方便,可是没有在规定的时间内完成555,今天上午有空便继续写,以下是完成后的代码: import java.util.Arr ...
- poj3187-Backward Digit Sums(枚举全排列)
一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角) 3 1 2 4 //1~n 全排列中的一个排列 4 3 6 7 ...
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
随机推荐
- mybatis操作动态表+动态字段+存储过程
存储过程 statementType="CALLABLE" <!-- 计算金额存储过程--> <update id="getCalcDistributo ...
- haproxy /admin跳转 不会在接口上再次加上admin
http://www.xx.com/admin/api/menu [root@wx03 mojo]# cat test.pl use Mojolicious::Lite; use JSON qw/en ...
- android ScrollView--Linearlayout可以上下拖动
动态添加: [java] view plaincopy <?xml version="1.0" encoding="utf-8"?> <Scr ...
- Js内存泄露问题总结
最近接受了一个Js职位的面试,问了很多Js的高级特性,才发现长时间使用已知的特性进行开发而忽略了对这门语言循序渐进的理解,包括Java我想也是一样,偶尔在Sun官方看到JDK6.0列举出来的new f ...
- WCF技术剖析之八:ClientBase<T>中对ChannelFactory<T>的缓存机制
原文:WCF技术剖析之八:ClientBase<T>中对ChannelFactory<T>的缓存机制 和传统的分布式远程调用一样,WCF的服务调用借助于服务代理(Service ...
- Netty源代码学习——ChannelPipeline模型分析
參考Netty API io.netty.channel.ChannelPipeline A list of ChannelHandlers which handles or intercepts i ...
- python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客
python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客 python datetime模块strptime/strptime form ...
- psl/sql本地与远程连接配置
一:下载Oracleclient 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-09748 ...
- 亚马逊AWS在线系列讲座——基于AWS云平台的高可用应用设计
设计高可用的应用是架构师的一个重要目标,可是基于云计算平台设计高可用应用与基于传统平台的设计有很多不同.云计算在给架构师带来了很多新的设计挑战的时候,也给带来了很多新的设计理念和可用的服务.怎样在设计 ...
- vcredist_x86.exe 静默安装方法
我们打包基于VC++开发的应用程序,我们会一同打包一个VC运行库,否则安装到一些非开发环境中,你的应用程序依然可以正确运行. Visual C++ 2008 Redistributable Packa ...