Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 需要在数字和星星之间建立一个金字塔_C_For Loop_Nested_Nested Loops - Fatal编程技术网

C 需要在数字和星星之间建立一个金字塔

C 需要在数字和星星之间建立一个金字塔,c,for-loop,nested,nested-loops,C,For Loop,Nested,Nested Loops,金字塔应该是这样的,使用指定的行数。我应该使用循环。不过,我真的很困惑如何在数字和星星之间添加适当数量的空格。另外,如何为每行显示正确的数字,以及使用什么条件以正确的数量停止数字。以下是我到目前为止的情况: 1 ** 2 3 *** **** { int userinput,rows,space,stspace,stars,num; //int startingnum; int i; printf(“输入行数:”); scanf(“%d”、&userin

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

    1
    **
 2       3
***      ****
{
int userinput,rows,space,stspace,stars,num;
//int startingnum;
int i;
printf(“输入行数:”);
scanf(“%d”、&userinput);
printf(“\n”);
对于(行=1;行这样

{

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("*");
                        }

                    }
        }
}
#包括
#包括
内部主(空){
int行;
printf(“输入行数:”;fflush(stdout);
scanf(“%d”,行和行);
printf(“\n”);
#如果0
当行数为2时绘制以下图形
[]表示一个元素。[]是为了便于理解一个元素
[    ][ 1  ]
[    ][ ** ]
[ 2  ][    ][ 3  ]
[*** ][    ][****]
#恩迪夫
int last_number=行*(行+1)/2;
int element_width=最后一个_数+1;
字符空白元素[元素宽度+1];//+1表示NUL
char stars_元素[元素_宽度+1];
字符工作元素[元素宽度+1];
char str_num[元素宽度+1];
void repeatStringOutput(常量字符*str,整数倍);
//制作空白元素
memset(空白元素“”,元素宽度);
空白元素[元素宽度]=0;
//初始化stars元素
strcpy(星型元素“*”);
int num=1;

对于(int r=1;r空间已声明,粘贴时代码的格式很奇怪,因此在修复时可能意外删除了声明。我将编辑。提示:last_number=rows*(rows+1)/2。element_max_width=last_number+1。
#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);
}