python全排列字典序输出 递归_全排列-字典序列、递归方法c语言实现
當前位置:我的異常網(wǎng)? C語言???全排列-字典序列、遞歸方法c語言實現(xiàn)
全排列-字典序列、遞歸方法c語言實現(xiàn)
www.MyException.Cn??網(wǎng)友分享于:2014-04-20??瀏覽:4次
全排列--字典序列、遞歸方法c語言實現(xiàn)
//字典序列方法
#include
int a[10],N;
void qsort(int a[],int left,int right)
{
int i,j,temp;
i=left;
j=right;
temp=a[left];
if(left>right)
return;
while(i!=j)
{
while(a[j]>temp &&j>i)
j--;
if(j>i)
a[i++]=a[j];
while(a[i]i)
i++;
if(j>i)
a[j--]=a[i];
}
a[i]=temp;
qsort(a,left,i-1);
qsort(a,i+1,right);
}
int fac()? //求N的階乘,即全排列的個數(shù)
{
int count=1,i=2;
while (i<=N)
{
count*=i;
i++;
}
return count;
}
void Reverse(int m,int n)
{
int temp;
while (m
{
temp=a[m];
a[m]=a[n];
a[n]=temp;
m++;
n--;
}
}
void Output()
{
int i;
for (i=0;i
{
printf("%3d",a[i]);
}
printf("\n");
}
void Fun()
{
int index1,index2,i,k,min,n,temp;? //index1為上述j下標,index2為上述k下標
n=fac();
for (k=1;k
{
for (index1=N-2;index1>=0;index1--)??? //求index1下標
{
if (a[index1]
{
break;
}
}
min=32767;
for (i=index1+1;i
{
if (a[i]>a[index1])
{
if (a[i]
{
min=a[i];
index2=i;
}
}
}
temp=a[index1];?????? //交換a[index1],a[index2]
a[index1]=a[index2];
a[index2]=temp;
Reverse(index1+1,N-1); //逆置a[index1]到a[N-1]的數(shù)
Output();? //輸出
}
}
int main()
{
int i;
while (printf("\n Please input N:? "),scanf("%d",&N)!=EOF)
{
printf("Please input %d numbers:\n",N);
for (i=0;i
{
scanf("%d",&a[i]);
}
qsort(a,0,N-1);? //先將N個數(shù)排序
printf("These numbers are:? \n");
for (i=0;i
{
printf("%3d",a[i]);
}
printf("\n");
Fun();
}
return 0;
}
//遞歸方法
#include
void permutation(int array[], int begin, int end)
{
int i;
if(begin == end){
for(i = 0; i <= end; ++i){
printf("%d",array[i]);
}
printf("\n");
return;
} else {
//for循環(huán)遍歷該排列中第一個位置的所有可能情況
for(i = begin; i <= end; ++i) {
swap(array[i], array[begin]);
permutation(array, begin + 1, end);
swap(array[i], array[begin]);
}
}
}
int main(int argc, char **argv)
{
int a[3] = {1, 2, 3};
permutation(a, 0, sizeof(a) / sizeof(int) - 1);
return 0;
}
文章評論
總結(jié)
以上是生活随笔為你收集整理的python全排列字典序输出 递归_全排列-字典序列、递归方法c语言实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 建行金钞有什么用
- 下一篇: 信用良好房贷被拒的原因