Symbolic triangle

Keywords: Programming

In the first line of the symbol triangle, there are n symbols composed of "+" and "-". Later, there are 1 fewer symbols in each line than those in the upper line. Under the two same signs, there are "+", and under the two different signs, there are "-". Calculate how many different symbol triangles contain the same number of "+" and "-". 1 symbol triangle when n=7 is as follows:
+ + - + - + + 
+ - - - - + 
- + + + - 
- + + - 
- + - 
- - 

Input

1 positive integer per line n < = 24, n = 0 exit

Output

n and the number of signed triangles

Sample Input

15
16
19
20
0

Sample Output

15 1896
16 5160
19 32757
20 59984

This problem should use deep search to construct the top layer, and then calculate other layers. At the same time, count one of the symbols, and finally determine whether the number of symbols is half of the total number of symbols. I won't explain the remaining details (such as whether the total number of symbols can be divided by 2, how to end the program when entering 0, etc.), but there will be notes in the code.

C++ version 1

DFS

#include<iostream>
using namespace std;
int n,total,sum;
int word[30][30];   //Array of stored symbols       
void wxy() //Function name has no meaning
{
    int x=n,y=0; //x is used to enumerate layers, y is used to calculate the number of negative signs of other layers
    while(x--) //Enumeration level
        for(int i=1;i<=x;i++)
        {
            word[x][i]=(word[x+1][i]+word[x+1][i+1])%2; //The ith symbol defining the n-x+1 layer
            if(word[x][i]) y++; //If word[x][i] is a negative sign, add 1 to the number of negative signs in other layers
        }
    if(sum+y==n*(n+1)/2/2) total++; //If the number of negative signs is half of the total number of signs, the case number plus 1 (using the sequence of equal difference numbers)
}
void dfs(int x)
{
    for(int i=0;i<2;i++) //0 is a positive sign, 1 is a negative sign
    {
        if(i) sum++; //Count the minus sign at the top of the topic
        word[n][x]=i;   //Defines whether the x-th symbol at the top is positive or negative
        if(x==n) wxy(); //If all symbols of the top layer are defined, calculate the number of minus signs of other layers
        else dfs(x+1); //Define the x+1 symbol at the top level
        if(i) sum--; //To flash back
    }
}
main()
{
    while(cin>>n&&n) //Input n, judge n is not 0
    {
        cout<<n<<" ";
        if((n*(n+1)/2)%2) {cout<<"0"<<endl;continue;} //Judge whether the total number of symbols can be divided by 2 (n * (n + 1) / 2 is calculated by using the sequence of equal difference)
        dfs(1);
        cout<<total<<endl;
        total=sum=0;
    }
}

C++ version two

Water problem

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
int n;
int a[30][30];

int main()
{
    while(scanf("%d",&n)!=EOF){
        if(n==0)    break;
        //int ans=0;
        cout <<n<<" ";
        switch (n){
                case 3:cout <<"4"<< endl;break;
                case 4:cout <<"6"<< endl;break;
                case 7:cout <<"12"<< endl;break;
                case 8:cout <<"40"<< endl;break;
                case 11:cout <<"171"<< endl;break;
                case 12:cout <<"410"<< endl;break;
                case 15:cout <<"1896"<< endl;break;
                case 16:cout <<"5160"<< endl;break;
                case 19:cout <<"32757"<< endl;break;
                case 20:cout <<"59984"<< endl;break;
                case 23:cout <<"431095"<< endl;break;
                case 24:cout <<"822229"<< endl;break;
                default :cout <<"0"<< endl;break;
                }

    }
    //cout << "Hello world!" << endl;
    return 0;
}

C++ version three

This should be OLE

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <math.h>
using namespace std;
int n;
int a[30][30];

int main()
{
    while(scanf("%d",&n)!=EOF){
        if(n==0)    break;
        int ans=0;
       
        for(int i=0;i<1<<n;i++){
            int cnt1=0,cnt0=0;
            for(int j=0;j<n;j++){
                a[0][j]=i>>j & 1;
                if( a[0][j]==1) cnt1++;
                if( a[0][j]==0) cnt0++;

                //cout << a[0][n-j-1];
            }


            for(int k=1;k<n;k++){
                for(int j=0;j<n-k;j++){
                    if(a[k-1][j]==a[k-1][j+1]){
                        a[k][j]=1;
                        cnt1++;
                    }else{
                        a[k][j]=0;
                        cnt0++;
                    }
                    //cout << a[0][n-j-1];
                }
                //cout<< endl;
            }

//            for(int k=0;k<n;k++){
//                for(int j=0;j<n-k;j++){
//
//                    if( a[k][j]==1) cnt1++;
//                    if( a[k][j]==0) cnt0++;
//                }
//                //cout<< endl;
//            }

            if(cnt0==cnt1) ans++;
            //cout<< endl;
        }
        cout <<n<<" " <<ans<< endl;
    }
    cout << "Hello world!" << endl;
    return 0;
}

 

Posted by cHinshaw on Sat, 14 Dec 2019 09:50:34 -0800