第m个全排列
#include<stdio.h> #include<string.h> int flag,n,m; int a[1002],sum,vis[1002]; void dfs(int k) { if(flag==1) return; if(k==n+1) { sum++; if(sum==m) { flag=1; for(int i=1;i<n;i++) printf("%d ",a[i]); printf("%d\n",a[n]); return ; } } else { for(int i=1;i<=n;i++) { if(!vis[i]) { vis[i]=1; a[k]=i; dfs(k+1); vis[i]=0; } } } } int main() { while(~scanf("%d %d",&n,&m)) { memset(vis,0,sizeof(vis)); sum=0; flag=0; dfs(1); } return 0; }
anytime you feel the pain.hey,refrain.don't carry the world upon your shoulders