/* 实现排列A(n,m)*/
#include "stdio.h"
int m,n,a[30];
long s=0;
int main()
{int p(int k);printf("input n(n<10):"); scanf("%d",&n);printf("input m(<1m<=n):"); scanf("%d",&m);p(1); //从第1个数开始 printf("\n总数为:%ld\n",s); //输出A(n,m)的值return 0;
}
//排列递归函数p(k)
int p(int k)
{int i,j,u;if(k<=m){for(i=1;i<=n;i++){a[k]=i; //探索第K个数赋值ifor(u=0,j=1;j<=k-1;j++)if(a[k]==a[j]) //若出现重复数字u=1; //若第K个数不可置,u=1if(u==0) //若第k个数可置,则检查是否到m个数{if(k==m) //若以到m个数,则打印出一个解{s++;printf(" ");for(j=1;j<=m;j++)printf("%d",a[j]);if(s%10==0) printf("\n");}else p(k+1); //若没到m个数,则探索下一个P(K+1) } }}return s;
}