惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
罗磊的独立博客
B
Blog
博客园_首页
A
About on SuperTechFans
有赞技术团队
有赞技术团队
V
V2EX
U
Unit 42
I
InfoQ
IT之家
IT之家
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
H
Help Net Security

BlackHoleMax

用 Podman 在家用电脑搭私人 WebDAV 书签同步服务 中国校服外包产业与“统一着装”的意识形态机制 Vue 响应式原理:从 2.x 的 Object.defineProperty 到 3.x 的 Proxy 赛里斯教育臃肿、企业文化与社会不平等的结构性困境 C语言学习(long和char强制类型转换) 意识形态国家机器(AIE)笔记 论阶级革命 论亲情 论盲目 当代唯分论,“什么书该读” 农场主与笨驴 如何成为一名安那其主义者 windows平台最好的包管理器:scoop 从Python3快速开始 正在施工🚧
C语言学习(帕斯卡三角)
BlackHoleMax · 2024-11-10 · via BlackHoleMax

关于帕斯卡三角,不做过多论述,可以先查看百度百科以及leetcode。我将演示几种常用方法打印帕斯卡三角。相较于官方题解我会写的更加易懂,便于读者学习。

  1. 解法1,使用数组存储每一列(如果你没有学习过组合数学,这是一种常用解法)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    #include <stdio.h>

    #define MAX 10

    int main() {
    int pascal[MAX][MAX] = {0};
    int rows, i, j;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);


    for (i = 0; i < rows; i++) {
    pascal[i][0] = 1;
    pascal[i][i] = 1;

    for (j = 1; j < i; j++) {
    pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
    }
    }


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

    for (j = 0; j < rows - i; j++) {
    printf(" ");
    }

    for (j = 0; j <= i; j++) {
    printf("%4d", pascal[i][j]);
    }
    printf("\n");
    }

    return 0;
    }

    另附leetcode的官方题解:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    int** generate(int numRows, int* returnSize, int** returnColumnSizes) {
    int** c = malloc(numRows * sizeof(int*));
    *returnSize = numRows;
    *returnColumnSizes = malloc(numRows * sizeof(int));
    for (int i = 0; i < numRows; i++) {
    (*returnColumnSizes)[i] = i + 1;
    c[i] = malloc((i + 1) * sizeof(int));
    c[i][0] = c[i][i] = 1;
    for (int j = 1; j < i; j++) {

    c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
    }
    }
    return c;
    }




  2. 解法2,使用二项式系数公式单独计算每一个值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    #include <stdio.h>


    int binomialCoeff(int n, int k);

    int main() {
    int rows, coef;


    printf("Enter the number of rows: ");
    scanf("%d", &rows);


    for (int i = 0; i < rows; i++) {

    for (int space = 1; space <= rows - i; space++)
    printf(" ");


    for (int j = 0; j <= i; j++) {
    coef = binomialCoeff(i, j);
    printf("%4d", coef);
    }
    printf("\n");
    }

    return 0;
    }


    int binomialCoeff(int n, int k) {
    int res = 1;
    if (k > n - k)
    k = n - k;
    for (int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
    }
    return res;
    }
  3. 解法3,基于解法2,我们也许还可以用递归来写

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    #include <stdio.h>


    void printPascalTriangle(int rows, int row);
    int binomialCoeff(int n, int k);

    int main() {
    int rows;


    printf("Enter the number of rows: ");
    scanf("%d", &rows);


    printPascalTriangle(rows, 0);

    return 0;
    }


    void printPascalTriangle(int rows, int row) {

    if (row >= rows)
    return;


    for (int space = 0; space < rows - row - 1; space++) {
    printf(" ");
    }


    for (int j = 0; j <= row; j++) {
    printf("%4d", binomialCoeff(row, j));
    }
    printf("\n");


    printPascalTriangle(rows, row + 1);
    }


    int binomialCoeff(int n, int k) {
    int res = 1;
    if (k > n - k)
    k = n - k;
    for (int i = 0; i < k; ++i) {
    res *= (n - i);
    res /= (i + 1);
    }
    return res;
    }