m0_66331107 2022-06-16 21:10 采纳率: 0%
浏览 10

回溯法做排列组合,n值大时没有输出

回溯法做排列组合,n值大时没有输出

我在用二叉树的回溯法做排列组合,递归左节点和右节点。
之后对n<6的时候做成功了,但是当n>6的时候,程序没有输出

img

当时我想是不是溢出了,但是我检查了下代码,没有对他原来所需内存进行分配(都没有数字)

#include<stdio.h>
  #include<stdlib.h>
  struct BiTNode{
      int *num;
      struct BiTNode *lchild,*rchild,*parent;
  };
  typedef struct BiTNode tree;
  int lengthf(int a1[])
  {
    int len=0;
    while (a1[len++]!='\0');
    len--;
    return len;
  }
int *copy(int a1[],int a2[],int len){
}
tree RCreatetree(tree *T,int i,int n){
    tree LCreatetree(tree *T,int i,int n);
    int lenp,len,j;                                          //注意这里的i是参数,而不是迭代数,j是迭代数!
    struct BiTNode *T1,*T2;
    T1=T->rchild;
    T1=(struct BiTNode*)malloc(sizeof(struct BiTNode));
    T1->parent=T;
    T2=T->rchild;
    T2=(struct BiTNode*)malloc(sizeof(struct BiTNode));
    T2->parent=T;
    lenp=lengthf(T->parent->num);
    //len=lengthf(T->num);
    //printf("长度为%d\n",lenp);
    if (i<n){
 
        //printf("1");
        T->num =(int *)malloc(sizeof(int) * (lenp+1));
        for(j=0; j<lenp; j++){
            T->num[j]=T->parent->num[j];
            //printf("输入值为%d\n",T->parent->num[j]);
        }
        T->num[lenp]=i;
        T->num[lenp+1]='\0';
        //printf("二次输入值为%d\n",T->num[lenp]);
        len=lengthf(T->num);
        //printf("新建数组的长度为%d\n",len);
 
        i++;
        RCreatetree(T1,i,n);
        LCreatetree(T2,i,n);
    }
    else{
        T->num =(int *)malloc(sizeof(int) * (lenp+1));
        for(j=0; j<lenp; j++){
            T->num[j]=T->parent->num[j];
            //printf("输入值为%d\n",T->parent->num[j]);
        }
        T->num[lenp]=i;
 
        for(j=0;j<(lenp+1);j++){
            printf("%d,",T->num[j]);
        }
        printf("\n");
    }
}
tree LCreatetree(tree *T,int i,int n){
    int lenp,j;
    struct BiTNode *T1,*T2;
    T1=T->rchild;
    T1=(struct BiTNode*)malloc(sizeof(struct BiTNode));
    T1->parent=T;
    T2=T->rchild;
    T2=(struct BiTNode*)malloc(sizeof(struct BiTNode));
    T2->parent=T;
    lenp=lengthf(T->parent->num);
 
    if (i<n){
        T->num =(int *)malloc(sizeof(int) * (lenp));
        for(j=0; j<lenp; j++){
            T->num[j]=T->parent->num[j];
        }
        T->num[lenp]='\0';
        i++;
        RCreatetree(T1,i,n);
        LCreatetree(T2,i,n);
    }
    else{
        T->num =(int *)malloc(sizeof(int) * (lenp));
        for(j=0; j<lenp; j++){
            T->num[j]=T->parent->num[j];
        }
        for(j=0;j<lenp;j++){
            printf("%d,",T->num[j]);
        }
        printf("\n");
    }
}
int main(){
    struct BiTNode *T,*T1,*T2;
    int i=1,n,lenp;
 
    printf("请输入排列组合数:\n");
    scanf("%d",&n);
 
    //初始化二叉树
    T=(struct BiTNode*)malloc(sizeof(struct BiTNode));
    T->num =(int *)malloc(sizeof(int) * (2));
    //T->num[0]=0;
    T->num[0]='\0';
    T1=T->rchild;
    T1=(struct BiTNode*)malloc(sizeof(struct BiTNode));
    T1->parent=T;
    T2=T->rchild;
    T2=(struct BiTNode*)malloc(sizeof(struct BiTNode));
    T2->parent=T;
 
    printf("所有的可能组合为:");
 
    RCreatetree(T1,i,n);
    LCreatetree(T2,i,n);
}
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-06-17 13:16
    关注
    
    #include<stdio.h>
    #include<stdlib.h>
    struct BiTNode{
        int *num;
        struct BiTNode *lchild,*rchild,*parent;
    };
    typedef struct BiTNode tree;
    int lengthf(int a1[])
    {
      int len=0;
      while (a1[len++]!='\0');
      len--;
      return len;
    }
    int *copy(int a1[],int a2[],int len){
    }
    void LCreatetree(tree *T,int i,int n);
    void RCreatetree(tree *T,int i,int n){
        int lenp,len,j;                                          //注意这里的i是参数,而不是迭代数,j是迭代数!
        struct BiTNode *T1,*T2;
    //  T1=T->rchild;
        T1=(struct BiTNode*)malloc(sizeof(struct BiTNode));
        T1->parent=T;
        T->rchild=T1;
    //  T2=T->rchild;
        T2=(struct BiTNode*)malloc(sizeof(struct BiTNode));
        T2->parent=T;
        T->lchild=T2;
        lenp=lengthf(T->parent->num);
        //len=lengthf(T->num);
        //printf("长度为%d\n",lenp);
        if (i<n){
    
            //printf("1");
            T->num =(int *)malloc(sizeof(int) * (lenp+1));
            for(j=0; j<lenp; j++){
                T->num[j]=T->parent->num[j];
                //printf("输入值为%d\n",T->parent->num[j]);
            }
            T->num[lenp]=i;
            T->num[lenp+1]='\0';
            //printf("二次输入值为%d\n",T->num[lenp]);
            len=lengthf(T->num);
            //printf("新建数组的长度为%d\n",len);
    
            i++;
            RCreatetree(T1,i,n);
            LCreatetree(T2,i,n);
        }
        else{
            T->num =(int *)malloc(sizeof(int) * (lenp+1));
            for(j=0; j<lenp; j++){
                T->num[j]=T->parent->num[j];
                //printf("输入值为%d\n",T->parent->num[j]);
            }
            T->num[lenp]=i;
    
            for(j=0;j<(lenp+1);j++){
                printf("%d,",T->num[j]);
            }
            printf("\n");
        }
    }
    void LCreatetree(tree *T,int i,int n){
        int lenp,j;
        struct BiTNode *T1,*T2;
    //  T1=T->rchild;
        T1=(struct BiTNode*)malloc(sizeof(struct BiTNode));
        T1->parent=T;
        T->rchild=T1;
    //  T2=T->rchild;
        T2=(struct BiTNode*)malloc(sizeof(struct BiTNode));
        T2->parent=T;
        T->lchild=T2;
        lenp=lengthf(T->parent->num);
    
        if (i<n){
            T->num =(int *)malloc(sizeof(int) * (lenp));
            for(j=0; j<lenp; j++){
                T->num[j]=T->parent->num[j];
            }
            T->num[lenp]='\0';
            i++;
            RCreatetree(T1,i,n);
            LCreatetree(T2,i,n);
        }
        else{
            T->num =(int *)malloc(sizeof(int) * (lenp));
            for(j=0; j<lenp; j++){
                T->num[j]=T->parent->num[j];
            }
            for(j=0;j<lenp;j++){
                printf("%d,",T->num[j]);
            }
            printf("\n");
        }
    }
    int main(){
        struct BiTNode *T,*T1,*T2;
        int i=1,n,lenp;
    
        printf("请输入排列组合数:\n");
        scanf("%d",&n);
    
        //初始化二叉树
        T=(struct BiTNode*)malloc(sizeof(struct BiTNode));
        T->num =(int *)malloc(sizeof(int) * 2);
        //T->num[0]=0;
        T->num[0]=0;
    //  T1=T->rchild;
        T1=(struct BiTNode*)malloc(sizeof(struct BiTNode));
        T1->parent=T;
        T->rchild=T1;
    //  T2=T->rchild;
        T2=(struct BiTNode*)malloc(sizeof(struct BiTNode));
        T2->parent=T;
        T->lchild=T2;
    
        printf("所有的可能组合为:\n");
    
        RCreatetree(T1,i,n);
        LCreatetree(T2,i,n);
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 6月16日

悬赏问题

  • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变
  • ¥15 算能的sail库的运用
  • ¥15 'Content-Type': 'application/x-www-form-urlencoded' 请教 这种post请求参数,该如何填写??重点是下面那个冒号啊
  • ¥15 找代写python里的jango设计在线书店
  • ¥15 请教如何关于Msg文件解析
  • ¥200 sqlite3数据库设置用户名和密码
  • ¥15 AutoDL无法使用docker install吗?
  • ¥15 cups交叉编译后移植到tina sdk的t113,只需要实现usb驱动打印机,打印pdf文件
  • ¥30 关于#wireshark#的问题:需要网络应用流量数据集需要做长度序列的实验,需要与应用产生的会话的数据包的长度,如视频类或者聊天类软件
  • ¥15 根据上述描述表示泥浆密度沿着管路的长度方向在不断变化,如何来表示泥浆密度随管路的变化(标签-matlab|关键词-流计算)