C# · 12月 20, 2021

使用C语言在屏幕上打印杨辉三角

@H_3010@杨辉三角

@H3010@1

@H3010@1 1

@H3010@1 2 1

@H3010@1 3 3 1

@H3010@1 4 6 4 1

@H3010@1 5 10 10 5 1

@H3010@第i行空格数:num-i

@H3010@

@H301_0@第i行输出数字个数:i

@H_3010@

@H3010@杨辉三角数组:arr[i][j] = arr[i – 1][j – 1] + arr[i – 1][j]

@H3010@

@H301_0@数组下标i代表输出杨辉三角的行数

@H_3010@

@H3010@数组下标j代表每行的第j个数字

@H301_0@#define _CRT_SECURE_NOWARNINGS

@H3010@#include<stdio.h>

@H3010@#include<stdlib.h>

@H3010@void Fun(int num) {

@H3010@int arr[100][100] = { 0,1 };

@H3010@for (int i = 1; i <= num; i++) {

@H3010@for (int j = 1; j <= i; j++) {

@H3010@arr[i][j] = arr[i – 1][j – 1] + arr[i – 1][j];

@H3010@}

@H3010@}

@H3010@for (int i = 1; i <= num; i++) {

@H3010@for (int k = 0; k <=num – i; k++) {

@H3010@printf(” “);//打印每行的空格

@H3010@}

@H3010@for (int j = 1;j<=i;j++){

@H3010@printf(“%4d”,arr[i][j]);//打印每行的杨辉三角数字

@H3010@}

@H3010@printf(“n”);

@H3010@}

@H3010@}

@H3010@int main() {

@H3010@int num;

@H3010@printf(“请输入打印的杨辉三角行数n”);

@H3010@scanf(“%d”,&num);

@H3010@Fun(num);

@H301_0@system(“pause”);

@H_3010@return 0;

@H3010@}

@H301_0@