开发者社区 问答 正文

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

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

展开
收起
知与谁同 2018-07-15 20:18:37 2260 分享 版权
3 条回答
写回答
取消 提交回答
  • 阿里云开发者社区运营负责人。原云栖社区负责人。
    12523你啊
    2019-07-17 22:54:52
    赞同 展开评论
  • //输出其中包含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;
    }
    2019-07-17 22:54:52
    赞同 展开评论
  • 云栖社区聚能聊、问答管理员~发福利、搞怪,八卦我来,论技术、发话题、写博客你上!
    #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;
    }

    -------------------------

    #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<<"自然数所有的排列组合为:\
    ";

    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("\
    ");

    } 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;
    }

    2019-07-17 22:54:52
    赞同 展开评论
问答分类:
问答地址: