当前位置:网站首页>C语言实现杨辉三角
C语言实现杨辉三角
2022-04-21 13:53:00 【头发没有代码多】
#define _CRT_SECURE_NO_WARNINGS 1
#define N 99
#include<stdio.h>
int main()
{
int i, j, k, h;
int a[N][N] = { 0 }; //定义二维数组
printf("请显示第X行杨辉三角:");
scanf("%d", &h); // 接收输入值
for (i = 0; i < h; i++) // 输入h行,对h行编辑
{
for (j = 0; j <h-i; j++) // h-i代表每一行打印空格得个数,随i即行数的增加而减少
printf(" ");
for (j = 0; j <= i; j++)
a[j][0] = 1;
a[j][j] = 1; // 每行的首数字和尾数字全为1
for (j = 2; j <= i; j++) // 从而开始是因为杨辉三角从第三行开始才有“肩膀数加和”
{
for (k = 1; k < i; k++) // 从第三行第二个数字才有“肩膀数相加”
a[j][k] = a[j - 1][k - 1] + a[j - 1][k]; //肩膀数相加
}
for (j = 0; j < i; j++)
{
printf("%6d", a[i][j]); // 打印,j为二维数组的列数
}
printf("\n");
}
return 0;
}
输入多少行就能打印出1-多少行,改变N的大小即可
看效果

版权声明
本文为[头发没有代码多]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_49449676/article/details/123671285
边栏推荐
- 双归上行--主备/负载
- Flow analysis (CTF)
- 使用js获取网页数据,并进行格式化输出(网页爬取)
- The stack concept is transformed into cyclic bracket matching inverse Polish expression simulation to achieve full dry goods
- socket组播出现的问题记录
- ForkJoin
- stm32的内存分布
- Zabbix5 series - monitoring MySQL (5.7 / 5.8 / MariaDB) (x)
- Digital signature, public-private key encryption and decryption of RSA
- RedisJSON:一个可以存储 JSON 的 Redis
猜你喜欢

双归上行--主备/负载

iscsi

NFS service, LVM capacity expansion

SQL injection vulnerability shooting range - sqli labs learning

< 2021SC@SDUSC Software engineering application and practice of Shandong University jpress code analysis (13)

Zabbix5 series - creating auto discovery templates (XVI)

求导法则 高阶导数

Zabbix5系列-创建自动发现模板 (十六)

Ssh server -- key authentication

K-means clustering based on word2vec
随机推荐
socket组播出现的问题记录
MySQL storage engine
Zabbix5 series - nail alarm (XV)
< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (10)
< 2021SC@SDUSC > Application and practice of software engineering in Shandong University jpress code analysis (12)
Content analysis and arrangement of Book 1 of the second phase (V) of Shandong University project training raspberry pie promotion plan
Zabbix5系列-接入Grafana面板 (十七)
RHCE builds a simple web site
MySQL 5.7 优化:Explain 执行计划近万字详解
web课程设计-照片记录网站(Flask)
Zabbix5 series - monitoring Hikvision camera (VII)
Understanding of machine learning
Preliminary test of glusterfs storage setup
Chapter II commercial password application and security evaluation policies and regulations on commercial password application and security evaluation - Summary of deleted version
C语言选择和循环经典习题
MarkDown格式
centos 离线安装mysql
The stack concept is transformed into cyclic bracket matching inverse Polish expression simulation to achieve full dry goods
CEPH maintenance command understanding
带你轻松玩转C语言scanf和getchar