全排列--字典序列、递归方法c语言实现
来源:互联网 发布:写一个数组 编辑:程序博客网 时间:2024/06/03 14:00
//字典序列方法
#include <stdio.h>
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]<temp &&j>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的阶乘,即全排列的个数
{
int count=1,i=2;
while (i<=N)
{
count*=i;
i++;
}
return count;
}
void Reverse(int m,int n)
{
int temp;
while (m<n)
{
temp=a[m];
a[m]=a[n];
a[n]=temp;
m++;
n--;
}
}
void Output()
{
int i;
for (i=0;i<N;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<n;k++) //n次循环
{
for (index1=N-2;index1>=0;index1--) //求index1下标
{
if (a[index1]<a[index1+1])
{
break;
}
}
min=32767;
for (i=index1+1;i<N;i++) //求index2
{
if (a[i]>a[index1])
{
if (a[i]<min)
{
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]的数
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<N;i++)
{
scanf("%d",&a[i]);
}
qsort(a,0,N-1); //先将N个数排序
printf("These numbers are: \n");
for (i=0;i<N;i++)
{
printf("%3d",a[i]);
}
printf("\n");
Fun();
}
return 0;
}
//递归方法
#include <stdio.h>
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循环遍历该排列中第一个位置的所有可能情况
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;
}
#include <stdio.h>
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]<temp &&j>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的阶乘,即全排列的个数
{
int count=1,i=2;
while (i<=N)
{
count*=i;
i++;
}
return count;
}
void Reverse(int m,int n)
{
int temp;
while (m<n)
{
temp=a[m];
a[m]=a[n];
a[n]=temp;
m++;
n--;
}
}
void Output()
{
int i;
for (i=0;i<N;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<n;k++) //n次循环
{
for (index1=N-2;index1>=0;index1--) //求index1下标
{
if (a[index1]<a[index1+1])
{
break;
}
}
min=32767;
for (i=index1+1;i<N;i++) //求index2
{
if (a[i]>a[index1])
{
if (a[i]<min)
{
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]的数
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<N;i++)
{
scanf("%d",&a[i]);
}
qsort(a,0,N-1); //先将N个数排序
printf("These numbers are: \n");
for (i=0;i<N;i++)
{
printf("%3d",a[i]);
}
printf("\n");
Fun();
}
return 0;
}
//递归方法
#include <stdio.h>
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循环遍历该排列中第一个位置的所有可能情况
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;
}
0 0
- 全排列--字典序列、递归方法c语言实现
- 全排列 递归实现 c 语言实现
- C语言递归实现全排列
- 全排列的实现方法--递归&字典序
- 递归实现全排列,字典序法实现全排列
- 全排列算法(字典序,递归实现)
- 全排列---递归实现与分析(C语言)
- 递归实例——C语言实现全排列
- 【C语言】递归实现元素的全排列和子集
- 实现全排列的两种算法:字典序列法以及递归算法(java)
- 全排列C语言实现
- C语言实现全排列
- C语言实现全排列
- C语言实现全排列
- c语言递归全排列问题
- 实现字典全排列
- 全排列-递归实现-C描述
- 元素全排列递归实现(C++)
- sicily 1422. Table Tennis 简单明了,非常容易看明白
- 支持一下
- poj3258 River Hopscotch
- Vote 2535
- Java中弱引用、软引用、虚引用及强引用的区别
- 全排列--字典序列、递归方法c语言实现
- Leetcode中的错误列表
- java笔试100题(1-50)
- Android开发之Intent详解
- “Gender”有一个无效 SelectedValue,因为它不在项目列表中。参数名: value
- centos修改系统时间
- Update CocoaPods问题小记
- office 2013 保存后 提示停止工作
- PO BO VO DTO POJO DAO概念及其作用(附转换图)