26、打印十行杨辉三角

/*
打印十行杨辉三角
 */

#include <stdio.h>
#include <stdlib.h>

void printMatrix(int lines)//动态方式,动态生成存储输出杨辉三角
{
   // int matrix[10][10] = {0};
    int *pMatrix = (int *) malloc(sizeof(int) * lines * lines);//动态申请一块内存,长度为一个int的字节数*lines*lines,内存首地址保存在变量 pMatrix中。
    for(int rowIndex = 0;rowIndex <= lines;rowIndex++)
    {
        for(int colIndex = 0;colIndex <= lines;colIndex++)
            *(pMatrix + rowIndex * lines + colIndex) = 1;
    }
    for(int rowIndex = 2;rowIndex < lines;rowIndex ++ )
    {
        for(int colIndex = 1;colIndex < rowIndex;colIndex++)
        {
            *(pMatrix + rowIndex * lines + colIndex) = *(pMatrix + (rowIndex - 1) * lines + colIndex - 1) + *(pMatrix + (rowIndex - 1) * lines + colIndex);
        }
    }
    for(int rowIndex = 0;rowIndex < lines;rowIndex++)
    {
        for(int colIndex = 0;colIndex <= rowIndex;colIndex++)
            printf("%d",*(pMatrix + rowIndex*lines + colIndex));
        printf("\n");
    }

}

int main()
{
    int lines = 0;
    scanf("%d", &lines);
    printMatrix(lines);
   // return 0;
}


 

有错误

posted @ 2021-09-25 21:27  叶梓渔  阅读(185)  评论(0)    收藏  举报