1. 程式人生 > >練習5-3 數字金字塔

練習5-3 數字金字塔

#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,m;  
    m = n;  
    for(i=1;i<=n;i++,m--)
    {  
        j=m-1; 	//the last line don't need the space! That means the space is one less than the number.
        for(;j>0;j--)  
            printf(" ");  
        for(k=i;k>0;k--)  
            printf("%d ", i);  // PTA receives the space after the number.
        printf("\n");  
    }  
}  

不得不說PAT的判題依據是需要仔細閱讀的,畢竟是機器判題,需要嚴格遵循規則。