设计递归算法生成n个元素的所有排列对象

如题,C/C++,在线等

#include<iostream>
#include<iterator>
#include<algorithm>
using namespace std;

template<class T>
void permutation(T list[], int k, int m)
{
if (k == m)
{
copy(list, list + m + 1, ostream_iterator<T>(cout, ""));    //将当前list排序
cout << endl;
}
else{
for (int i = k; i <= m; i++)
{
swap(list[i], list[k]);     //将下标为i的元素交换到k位置,类似从list[k:m]中剔除操作
permutation(list, k + 1, m);
swap(list[i], list[k]);
}
}
}

int main(int argc, char* argv[])
{
char arr[3] = { 'a', 'b', 'c' };
cout << "排序结果如下:" << endl;
permutation(arr, 0, 2);
return 0;

}

扩展资料

递归,在数学与计算机科学中,是指在函数的定义中使用函数自身的方法。也就是说,递归算法是一种直接或者间接调用自身函数或者方法的算法。

通俗来说,递归算法的实质是把问题分解成规模缩小的同类问题的子问题,然后递归调用方法来表示问题的解。

递归的基本原理

第一:每一级的函数调用都有自己的变量。

第二:每一次函数调用都会有一次返回。

第三:递归函数中,位于递归调用前的语句和各级被调用函数具有相同的执行顺序。

第四:递归函数中,位于递归调用后的语句的执行顺序和各个被调用函数的顺序相反。

第五:虽然每一级递归都有自己的变量,但是函数代码并不会得到复制。

参考资料来源:

百度百科——递归算法

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-05-05
//输出其中包含n个数的所有排列
#include
using
namespace
std;
int
data[100];
void
DPpl(int
num,int
m,int
n,int
depth)
{
if(depth==n)
{
for(int
i=0;i
评论
0
0
加载更多
第2个回答  2009-12-21
//给你m个数,输出其中包含n个数的所有排列
#include <iostream>
using namespace std;

int data[100];
void DPpl(int num,int m,int n,int depth)
{
if(depth==n)
{
for(int i=0;i<n;i++)
cout<<data[i]<<" ";
cout<<endl;
}
for(int j=0;j<m;j++)
{
if((num&(1<<j))==0)
{
data[depth]=j+1;
DPpl(num+(1<<j),m,n,depth+1);
}
}
}
int main()
{
//DPpl(0,5,1,0);
//DPpl(0,5,2,0);
DPpl(0,5,3,0);//5个数输出包含其中3个数的排列
//DPpl(0,5,4,0);
//DPpl(0,5,5,0);
return 0;
}
程序不长,是我以前帮别人写的。
在我空间里面有不过程序就是上面的。
http://hi.baidu.com/huifeng00/blog/item/cd01b0152e69f503c93d6d8f.html
第3个回答  推荐于2016-08-20
#include<iostream>
#include<cmath>
using namespace std;

int count(int n) //算n的阶乘——因为n个数能组成n!个数
{
if(n<1)
{
cout<<"输入也错!";
exit(0);
}
else
if(n==1)
return 1;
else
return count(n-1)*n;
}

int pow10(int n) //算出10的n次方
{
int a;
if(n==0)
return 1;
else
for(int i=1;i<=n;i++)
a=10*pow10(n-1);
return a;
}
int * comm(int n) //组合n!个数(这里用递归算)
{
int *a=new int[count(n)];
if(count(n)==1)
a[0]=1;
else
{
int *b=new int[count(n-1)];
b=comm(n-1);
for(int i=0;i<count(n-1);i++)
for(int j=0;j<n;j++)
a[i*n+j]=(b/pow10(j)*10+n)*pow10(j)+b%pow10(j);
}
return a;
}
void main()
{
int n;
cout<<"请输入n=";
cin>>n;
int *a=new int[count(n)];
a=comm(n);
cout<<"1-"<<n<<"自然数所有的排列组合为:\n";
for(int i=0;i<count(n);i++)
cout<<a<<" ";
}

=======================================
#define MAX 1000
#include<stdio.h>

void DispArrangement(int a[MAX], int n, int deepth)
{
int i, temp;
if(deepth == 1) {
for(i = 1; i <= n; i ++) {
printf("%d", a);
}
printf("\n");
} else {
for(i = 1; i <= deepth; i ++ ){
temp = a[n - deepth + 1];
a[n - deepth + 1] = a[n - deepth + i];
a[n - deepth + i] = temp;
DispArrangement(a, n, deepth - 1);
temp = a[n - deepth + 1];
a[n - deepth + 1] = a[n - deepth + i];
a[n - deepth + i] = temp;
}
}
}

int main(void)
{
int i, n, a[MAX];
scanf("%d", &n);
for(i = 1; i <= n; i ++ ) {
a = i;
}
DispArrangement(a, n, n);
return 0;
}本回答被提问者采纳
第4个回答  2019-11-10
#include<stdio.h>
#include<stdlib.h>
struct
cc{
char
c;
int
s;
};
void
f(cc*
sc,
char*
t,
int
n,
int
l)
{
int
i;
for(i=0;i<n;i++){
if(sc[i].s
==
1){
sc[i].s
=
0;
t[l]
=
sc[i].c;
if(l<n-1)
f(sc,
t,
n,
l+1);
sc[i].s
=
1;
}
}
if(l
==
n-1){
t[n]='\0';
printf("%s\n",
t);
}
}
int
main()
{
int
n,i;
scanf("%d",
&n);
char*
arr
=
(char*)malloc(sizeof(char)*n);
scanf("%s",
arr);
cc*
ac
=
(cc*)malloc(sizeof(cc)*n);
for(i
=
0
;i<n;i++){
cc
c;
c.c
=
arr[i];
c.s
=
1;
ac[i]
=
c;
}
char*
temp
=
(char*)malloc(sizeof(char)*(n+1));
f(ac,
temp,
n,
0);
return
0;
}