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

推荐订阅源

S
SegmentFault 最新的问题
Spread Privacy
Spread Privacy
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
SecWiki News
SecWiki News
腾讯CDC
P
Privacy International News Feed
Webroot Blog
Webroot Blog
J
Java Code Geeks
爱范儿
爱范儿
A
About on SuperTechFans
S
Secure Thoughts
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
Cloudbric
Cloudbric
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Security Latest
Security Latest
Forbes - Security
Forbes - Security
小众软件
小众软件
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Threatpost
量子位
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Google Online Security Blog
Google Online Security Blog
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
V
Visual Studio Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 聂微东
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs

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;
    }