Exercise 5-3 Number Pyramid (15 points)

Function interface definition

void pyramid( int n );

Where n is the parameter passed in by the user, which is a positive integer of [1, 9]. The function is required to
print out n rows of digital pyramids in the format shown in the example . Note that each number is followed by a space.

Sample referee test procedure

#include <stdio.h>
 
void pyramid( int n );
 
int main()
{
    
        
    int n;
 
    scanf("%d", &n);
    pyramid(n);
 
    return 0;
}
 
/* 你的代码将被嵌在这里 */

Input sample

5

Sample output

    1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5 

my answer

void pyramid( int n ){
    
    
    int i,j;
    for(i=1; i<=n; i++){
    
    
        for(j=n-i; j>0; j--){
    
    
            printf(" ");
        }
        for(j=1; j<=i; j++){
    
    
            printf("%d ", i);
        }
        printf("\n");
    }
}

Guess you like

Origin blog.csdn.net/qq_43191910/article/details/104834179