118. Pascal's Triangle C language

Given a non-negative integer numRows, generation ago with numRows Yang Hui Triangle.

In Pascal's Triangle, each number is the number of its top left and top right and.

Example:

Input: 5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

Here is my routine Solution: did not use the pointer, but the force of the return type of wear is this: int ** generate (int numRows, int * returnSize, int ** returnColumnSizes)

#include <stdio.h>
int main()
{
int i ,j,row ;
int a[100][100]={0};
printf("请输入行数:");
scanf("%d",&row);
for(i=0;i<row;i++)
{
for(j=0;j<i+1;j++)
{
if(j==0||j==i)
{
a[i][j]=1;
}
else
{
a[i][j]=a[i-1][j-1]+a[i-1][j];
}
printf("%d ",a[i][j]);
}
printf("\n");
}
}

Here is the solution fasten force: hey pointer pointer is really very big head, have to pay more to see

int** generate(int numRows, int* returnSize, int** returnColumnSizes){
     * ReturnSize = numRows; // returnSize is a pointer to an integer representing the number of rows returned Pascal's triangle = input numRows
    * ReturnColumnSizes = (int *) malloc (numRows * sizeof (int)); // returnColumnSizes pointing a pointer to the array, the number of elements of the corresponding row of the array elements
    int ** res = (int **) malloc ((* returnSize) * sizeof (int *)); // res is a pointer to the array constituted by the pointer, each pointer corresponding to pointing triangle number line; res is a two-dimensional array
    int i = 0;
    for(; i < *returnSize; i++){
        (*returnColumnSizes)[i] = i + 1;
        res[i] = (int*)malloc((*returnColumnSizes)[i] * sizeof(int));
        res[i][0] = 1;
        res[i][i] = 1; 
    }
    for(i = 2; i <= numRows - 1; i++){
        for(int j = 1; j < i; j++){
            res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
        }
    }
    return res;
}
First, you look at the for loop, it is to give the two sides of the elements of Pascal's Triangle assignment 1. (* ReturnColumnSizes) [i] = i + 1; the number given to the corresponding row elements,
res [i] = (int *) malloc ((* returnColumnSizes) [i] * sizeof (int)); and according to the number of elements to allocate storage space. The first two elements of each row and then assign a value of 1.

 

Guess you like

Origin www.cnblogs.com/cocobear9/p/12324374.html