Today is the third day : practice - array - Search of Yang Hui triangle

         hello everyone , I am C Language Xiaobai -Haven, Yesterday, I learned to code a simple self-made project as my homework last night , It's the super reduced version of the probability system and the knowledge learned before .

         It's basically an ordinary card drawing system , What I do is also a word game based on probability and conditional selection , I chose the strange talk of zoo rules , However, due to the large selection of topics and manpower , time , insufficient fund , I can only make a super deletion and magic revision , It's too late , So I didn't write a daily practice .

        subject : Enter a positive integer n, Before printing n Yang Hui triangle .

                         Let's talk about Yang Hui triangle first :


                                                                   1        1

2        1

                                                         1        3       
3        1

                                                    1        4        6       
4        1

         Let's analyze it first , The edge is fixed 1, The middle can form a small triangle , Each value is the sum of two sharp corner values above its head , for example :

        2==1+1; 3==1+2;4==1+3;6==3+3;

thinking process :

        1. My first reaction is that since the following values depend on the above values , Then I'll use two arrays a[],b[] Based on behavior , Enter alternately line by line , output .

        At the same time, it is found that the first row has a value , The second line has two values , The third line has three values .....

         At first a[0] assignment 1, After outputting the first line, give two edges to the next line b[0] And b[m] assignment , Then assign the intermediate value according to the conditions . Using up-down switching device , Make the next output b[], Give me two a[0] And a[m] assignment , Assign intermediate values according to conditions . This cycle continues .

        2. There is another way to use two-dimensional arrays , Direct the entire front n All rows are calculated , It is divided into two modules: triangle on both sides and triangle in the middle , You can try this yourself .

        3. Another way is to use a one-dimensional array a[n] Calculated , I don't know this in detail , If you are interested, you can write it yourself .

/* task : Print Yang Hui triangle Enter a positive integer n, Before printing Yang Hui triangle n that 's ok ; */ #include<stdio.h> int main() {
/********* Definition and input module ********/ int n,m,i,flag=0; printf("please input a data:");
scanf("%d",&n); int a[n],b[n]; a[0]=1; /*********** Execution module **********/
for(m=1;m<=n;m++) { if(flag==0) { for(i=0;i<m;i++)//------------ yes a[n] Output module for {
printf("%-4d",a[i]); } printf("\n"); b[0]=b[m]=1;//---------------- yes b[n] Input module for
for(i=1;i<m;i++) { b[i]=a[i-1]+a[i]; } flag=1;//--------------------- Up-down conversion device }
else if(flag==1) { for(i=0;i<m;i++)//------------ yes b[n] Output module for {
printf("%-4d",b[i]); } printf("\n"); a[0]=a[m]=1;//---------------- yes a[n] Input module for
for(i=1;i<m;i++) { a[i]=b[i-1]+b[i]; } flag=0;//--------------------- Up-down conversion device } }
return 0; }