需要建立一个数字和星星之间有空格的金字塔

时间:2017-04-16 11:48:32

标签: c for-loop nested nested-loops

    1
    **
 2       3
***      ****

使用指定的行数,金字塔应该看起来像这样。我应该用于循环。不过,我真的很困惑如何在数字和星星之间添加适当数量的空格。此外,如何为每一行显示正确的数字,以及使用什么条件来停止正确数量的数字。以下是我到目前为止的情况:

{

int userinput, rows, space, stspace, stars, num;

//int startingnum;

int i;

printf("Enter the number of rows: ");

scanf("%d",&userinput);

printf("\n");

    for(rows=1; rows<=userinput; rows++)
    //printf("\n");
        {
            printf("\n");
            //for(i=1; i <= rows; i++)
            //{
            //startingnum += i;
            //}

                if(rows%2!=0)
                    {
                    for(space=1; space<=(userinput)/(rows+1); space++)
                    printf("-");
                    {
                        for(num=1; num<=rows; num++)
                        printf("%d",num);
                    }
                    }
                else
                    {
                    for(stspace=1; stspace<=(userinput)*(rows); stspace++)
                    printf("-");
                        {
                        for(stars=num+1; stars<=num+rows+1;stars++)
                            printf("*");
                        }

                    }
        }
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

像这样

#include <stdio.h>
#include <string.h>

int main(void){
    int rows;

    printf("Enter the number of rows: ");fflush(stdout);
    scanf("%d", &rows);
    printf("\n");
#if 0
Draw the following when rows is 2
[    ] represents one element. [] is for easy understanding for a element

[    ][ 1  ]
[    ][ ** ]
[ 2  ][    ][ 3  ]
[*** ][    ][****]
#endif
    int last_number = rows * (rows + 1) / 2;
    int element_width = last_number + 1;

    char blank_element[element_width +1];//+1 for NUL
    char stars_element[element_width +1];
    char work_element[element_width +1];
    char str_num[element_width +1];
    void repeatStringOutput(const char *str, int times);

    //make blank element
    memset(blank_element, ' ', element_width);
    blank_element[element_width] = 0;
    //initialize stars element
    strcpy(stars_element, "*");

    int num = 1;
    for(int r = 1; r <= rows; ++r){
        //print pre-blank-element
        repeatStringOutput(blank_element, rows - r);
        for(int c = 0; c < r; ++c){
            int len = sprintf(str_num, "%d", num + c);
            if(c)//print blank element between stars element
                fputs(blank_element, stdout);
            strcpy(work_element, blank_element);//fill spaces
            memcpy(work_element + (element_width - len)/2, str_num, len);//centering
            fputs(work_element, stdout);
        }
        puts("");
        repeatStringOutput(blank_element, rows - r);
        for(int c = 0; c < r; ++c){
            strcpy(stars_element + num++, "*");
            if(c)
                fputs(blank_element, stdout);
            strcpy(work_element, blank_element);
            memcpy(work_element + (element_width - num)/2, stars_element, num);
            fputs(work_element, stdout);
        }
        puts("");
    }
}

void repeatStringOutput(const char *str, int times){
    while(times--)
        fputs(str, stdout);
}