练习5-3 数字金字塔 (15point(s)).c

本题要求实现函数输出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 
//   Date:2020/3/30
//   Author:xiezhg5
#include <stdio.h>

void pyramid( int n );

int main()
{    
    int n;

    scanf("%d", &n);
    pyramid(n);

    return 0;
}

/* 你的代码将被嵌在这里 */
void pyramid( int n )
{
	int i,j,k;
	for(i=1;i<=n;i++)            //i控制行 
	{
		//j随i的变化而变化 
		for(j=1;j<=n-i;j++)
			printf(" ");      //打印首位数字前的空格 
		for(k=1;k<=i;k++)    //j控制列 
			printf("%d ",i);
		printf("\n");
	}
}
发布了134 篇原创文章 · 获赞 94 · 访问量 3168

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/105206714
今日推荐