c语言实现菱形

C语言 实现菱形

//think again
// col --> the sum of cols
//curCol --> current printing col
//for diamond print , this is a circulation consist of cols print
// every cols print associate with current cols and the sum of cols
//the mid --> acol/2 + 1
//first of all , we should judge the number , if negative , remind and exit
//i separate the whole print to two parts 
//first 1 ~ mid 
//second mid ~ col
//think about the every col 's print
//in first part , the number of space is
//        mid - curCol , the number of * is 2 * curCol - 1 
//in second part , the number of space is 
//        ( (col) - 2 * ( curCol - ( col / 2 + 1 ) ) )
#include
int main()
{
    int col ;
    int count ;
    printf(" please input cols : ") ;
    scanf("%d",&col) ;
    if( ( col%2 == 0 ) || ( col <= 0  ) )
    {
        printf(" col of diamond should be a odd \n") ;
        return 0 ;
    }
    int curCol = 1 ;
    while( curCol <= col )
    {
        if( curCol <= ( col/2 + 1 ) )
        {
            for( count = 0 ; count < ( ( ( col / 2 ) + 1 ) - curCol ) ; count++ )
            {
                printf(" ") ;
            }
            //printf("\n") ;
            for( count = 0 ; count < 2 * curCol - 1 ; count++ )
            {
                printf("*") ;
            }
            printf("\n");  
        }
        else
        {
            for( count = 0 ; count < ( curCol - ( col/2 + 1 ) ) ; count++ )
            {
                printf(" ");
            }
            for( count = 0 ; count < ( (col) - 2 * ( curCol - ( col / 2 + 1 ) ) ) ; count++  )
            {
                printf("*");
            }
            printf("\n");
        }
        curCol++;
    }
    return 0 ;
}

当考虑图形打印的程序时,我们最好第一步考虑到整个打印的规律,看看每行*,space数量与整个图形组成的关系,像这种x轴对称的图形,上下两部分的打印代码之间有着很强的联系,做题目先思考“规律”,再写代码,永远记住写代码是最简单的事情,思考是关键

你可能感兴趣的:(C语言)