6 Ways to Language C Pascal's Triangle of six methods of Pascal's Triangle C language requirements

6 Ways to C language requirements of Pascal's Triangle

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Triangle, its essence is the binomial (a + b) to expand the power of n coefficients arranged in a triangle, which is characterized by a right and left sides of all, starting from the second row, every number of It is on one line and two numbers adjacent. This topic commonly used in programming exercises.

The following six different solution is given.

A Solution

#include <stdio.h>

main()

{ int i,j,n=0,a[17][17]={0};

while(n<1 || n>16)

{Printf ( "Please enter the number of rows of Pascal's triangle:");

scanf("%d",&n);

}

for(i=0;i<n;i++)

a [i] [0] = 1; / * first column of a full set * /

for(i=1;i<n;i++)

for(j=1;j<=i;j++)

a [i] [j] = a [i-1] [j-1] + a [i-1] [j]; / * Each number is the number of two and above * /

for (i = 0; i <n; i ++) / * output Pascal triangle * /

{ for(j=0;j<=i;j++)

printf("%5d",a[i][j]);

printf("\n");

}

}

Comments: First, the general solution most likely to think of the solution, independent function of each part, the program easy to understand.

Solution two

#include <stdio.h>

main()

{ int i,j,n=0,a[17][17]={1};

while(n<1 || n>16)

{Printf ( "Please enter the number of rows of Pascal's triangle:");

scanf("%d",&n);

}

for(i=1;i<n;i++)

{A [i] [0] = 1; / * first column of a full set * /

for(j=1;j<=i;j++)

a [i] [j] = a [i-1] [j-1] + a [i-1] [j]; / * Each number is the number of two and above * /

}

for (i = 0; i <n; i ++) / * output Pascal triangle * /

{ for(j=0;j<=i;j++)

printf("%5d",a[i][j]);

printf("\n");

}

}

Reviews: two Solutions Xu is a solution on the basis of, the first column is set to the move command a double loop below, is reduced by one cycle. Note that changes to initialize the array.

Solution three

#include <stdio.h>

main()

{ int i,j,n=0,a[17][17]={0,1};

while(n<1 || n>16)

{Printf ( "Please enter the number of rows of Pascal's triangle:");

scanf("%d",&n);

}

for(i=1;i<=n;i++)

for(j=1;j<=i;j++)

a [i] [j] = a [i-1] [j-1] + a [i-1] [j]; / * Each number is the number of two and above * /

for (i = 1; i <= n; i ++) / * output Pascal triangle * /

{ for(j=1;j<=i;j++) printf("%5d",a[i][j]);

printf("\n");

}

}

Reviews: Method three is based on a solution, the two, the first column 1 is set to remove the command, changes in attention array initialization.

For Four

#include <stdio.h>

main()

{ int i,j,n=0,a[17][17]={0,1};

while(n<1 || n>16)

{Printf ( "Please enter the number of rows of Pascal's triangle:");

scanf("%d",&n);

}

for(i=1;i<=n;i++)

{ for(j=1;j<=i;j++)

{A [i] [j] = a [i-1] [j-1] + a [i-1] [j]; / * Each number is the sum of two numbers above * /

printf ( "% 5d", a [i] [j]); / * output Pascal triangle * /

}

printf("\n");

}

}

Comments: for Four solution is based on the three, merge the computing and printing in a double loop.

Solution of the Five

#include <stdio.h>

main()

{ int i,j,n=0,a[17]={1},b[17];

while(n<1 || n>16)

{Printf ( "Please enter the number of rows of Pascal's triangle:");

scanf("%d",&n);

}

for(i=0;i<n;i++)

{ b[0]=a[0];

for(j=1;j<=i;j++)

b [j] = a [j-1] + a [j]; / * Each number is the sum of two numbers above * /

for(j=0;j<=i;j++)

/ * Output Pascal's Triangle * /

{A [j] = b [j]; / * assign the calculated new line a, and the print for the next calculation * /

printf("%5d",a[j]);

}

printf("\n");

}

}

Comments: The Solution for Four are used to a two-dimensional array, take up more space. The solution uses two five one-dimensional arrays.

Solution six

#include <stdio.h>

main()

{ int i,j,n=0,a[17]={0,1},l,r;

while(n<1 || n>16)

{Printf ( "Please enter the number of rows of Pascal's triangle:");

scanf("%d",&n);

}

for(i=1;i<=n;i++)

{ l=0;

for(j=1;j<=i;j++)

{ r=a[j];

a [j] = l + r; / * Each number is the sum of two numbers above * /

l=r;

printf ( "% 5d", a [j]); / * output Pascal triangle * /

}

printf("\n");

}

}

Comments: The six solution uses a one-dimensional array and two temporary variables.

Published 25 original articles · won praise 5 · Views 332

Guess you like

Origin blog.csdn.net/weixin_44602007/article/details/105020562