hdu 1027 Ignatius and the Princess II
来源:互联网 发布:货拉拉软件 编辑:程序博客网 时间:2024/05/16 06:09
题意:求出数字1-n的第m个全排列
1.调用函数
#include<iostream>#include<algorithm>#define maxn 1000+5using namespace std;int n,m;int ans[maxn];int main(){while(cin>>n>>m){for(int i=1;i<=n;i++) ans[i]=i;while(--m){next_permutation(ans+1,ans+n+1);}cout<<ans[1];for(int i=2;i<=n;i++) cout<<" "<<ans[i];cout<<endl;}return 0;}
2.手写函数
#include<iostream>#define maxn 1000+5using namespace std;int ans[maxn];int visit[maxn];int n,m,sum;int flag;void print(){cout<<ans[1];for(int i=2;i<=n;i++) cout<<" "<<ans[i];cout<<endl;}void dfs(int d){if(d==n+1){if(sum==m){print();flag=1;return;}else sum++;}for(int i=1;i<=n;i++){if(!visit[i]){ans[d]=i;visit[i]=1;dfs(d+1);visit[i]=0;if(flag) return; }} }int main(){cin.sync_with_stdio(false);while(cin>>n>>m){for(int i=1;i<=n;i++) ans[i]=i,visit[i]=0;sum=1;flag=0;dfs(1);}return 0;}
0 0
- hdu 1027 ignatius and the princess ii
- HDU 1027 Ignatius and the Princess II
- HDU 1027 ( Ignatius and the Princess II )
- HDU 1027 Ignatius and the Princess II
- HDU 1027 Ignatius and the Princess II
- hdu 1027 Ignatius and the Princess II
- hdu 1027 Ignatius and the Princess II
- hdu 1027 Ignatius and the Princess II
- hdu 1027 Ignatius and the Princess II
- hdu 1027 Ignatius and the Princess II
- Ignatius and the Princess II hdu 1027
- HDU 1027 Ignatius and the Princess II
- HDU 1027 Ignatius and the Princess II
- hdu 1027 Ignatius and the Princess II
- HDU 1027 Ignatius and the Princess II
- HDU 1027 Ignatius and the Princess II
- HDU 1027 Ignatius and the Princess II
- hdu 1027 Ignatius and the Princess II
- VS2010灵活运用快捷操作功能(总结)
- Scala学习笔记(八) - 特质
- 第12章 内中断
- POJ 2236 Wireless Network 并查集
- Scala学习笔记(九) - 包和引用
- hdu 1027 Ignatius and the Princess II
- 黑马程序员——io之第四部分
- 装饰模式
- POJ 3253----Fence Repair 要用到优先队列!!!
- 第13章 int指令
- c语言产生随机数
- 网络地址转换
- 【Servlet】Javaweb中获取任意个参数
- 从零开始学习,Apache Spark源码走读(一)