本题要求实现函数输出n行数字金字塔。
void pyramid( int n );
其中n
是用户传入的参数,为[1, 9]的正整数。要求函数按照如样例所示的格式打印出n
行数字金字塔。注
意每个数字后面跟一个空格。
#include <stdio.h>
void pyramid( int n );
int main()
{
int n;
scanf("%d", &n);
pyramid(n);
return 0;
}
/* 你的代码将被嵌在这里 */
5
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
看不懂没关系,画图理解很简单的
void pyramid(int n) { int line,kong,imp; for (line = 1;line<=n;line++) { for (kong = n-line;kong>0;kong--)//打印第一个大空格,等待imp打印完补充 printf(" "); for (imp = 1;imp <=line;imp++)//优先级相同 { printf("%d ",line); } printf("\n"); } }
浙大版《C语言程序设计(第3版)》题目集 练习5-3 数字金字塔
原文:https://www.cnblogs.com/elapstjtl/p/13798980.html