Finding all combinations of well-formed brackets

后端 未结 29 1650
盖世英雄少女心
盖世英雄少女心 2020-11-28 02:34

This came up while talking to a friend and I thought I\'d ask here since it\'s an interesting problem and would like to see other people\'s solutions.

The task is to

29条回答
  •  天涯浪人
    2020-11-28 03:05

    //C program to print all possible n pairs of balanced parentheses  
    
    
    #include
    
    void fn(int p,int n,int o,int c);
    
    void main()
    {
        int n;
        printf("\nEnter n:");
        scanf("%d",&n);
        if(n>0)  
            fn(0,n,0,0);
    }
    
    void fn(int p,int n,into,int c)
    {  
        static char str[100];
        if(c==n)
        {
            printf("%s\n",str);
            return;
        }
        else
        {
            if(o>c)
            {
                str[p]='}';
                fn(p+1,n,o,c+1);
            }
            if(o

提交回复
热议问题