日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python全排列字典序输出 递归_全排列-字典序列、递归方法c语言实现

發(fā)布時間:2024/10/12 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python全排列字典序输出 递归_全排列-字典序列、递归方法c语言实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

當前位置:我的異常網(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。