嵌套For循环,C语言中的X-mas树 [英] Nested For loops, X-mas tree in C

查看:119
本文介绍了嵌套For循环,C语言中的X-mas树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个用C语言创建圣诞树的任务,我知道这已经死了,但是必须满足一些条件,这使我感到困惑,我什至不知道从哪里开始.

So I have the task of creating a Christmas tree in C, I know this has been done to death but there are some conditions that have to be meet that leave me beyond confused, I don't even know where to start.

因此,我们向用户询问级别数(一层中的几行),然后是层数.

So we ask the user for the number of levels(how many lines in the layer) and then the number of layers.

现在,每一层中第一行之后的每一行将在第一行的每一侧添加2"*"(这只是带有一个"*"的行.)然后执行此操作,直到层相遇,然后开始下一层.当我开始新层时,我们从上一层的最后一个级别的每一侧减去4(2"*",然后重复向每一侧添加1"*"的过程,直到满足级别数为止(级别数是一开始就确定的,并且是恒定的.)

Now, each line after the first in each layer will add 2 " * " one to each side of the first( which is just a line with one " * ".) And we do this until the number of levels in the layer is meet, then the next layer is started. When I new layer is started we subtract 4( 2" * " from each side, of the last level in the previous layer, and then repeat the process of adding 1 " * " to each side until the number of levels is meet( the number of levels is decided upon in the beginning and is constant.)

最后,最后一部分是由#"制成的,宽度为3,高度为4的树干的树.我不知道应该如何设置这些循环,我将发布到目前为止的操作(我不确定该如何进行)

Finally the last part is finishing off the tree of the tree with a width 3, height 4 trunk made of " # ". I have no idea how I'm supposed to be setting up these loops, I'll post what I've done so far( not much I'm unsure how to proceed)

#include <stdio.h>
#include <stdlib.h>


int main(){


int levels;
int levelcount;
int layers;
int i;
int j;

    printf(" How many levels should this tree have?\n");
    scanf("%d\n", levels);

    printf(" How many layers should this tree have?\n");
    scanf("%d\n");

for (level = 0;level <= levelcount ; levelcount++){


}








return 0;

}

例如,将一个具有3个级别的2层放出的样本为

for example a sample out put with 2 layers of 3 levels would be

     *
    ***
   *****
     *
    ***
   *****
    ###
    ###
    ###
    ###


以下是我现在所在位置的一些更新代码


Below is some updated code for where i'm at now

#include <stdio.h>
#include <stdlib.h>


int main(){


int level;
int levelcount;
int layercount;
int layer;
int star;
int starcount;
int space;
int spacecount;
int spacenumber;

    printf("How many levels should this tree have?\n");
    scanf("%d\n", &level);

    printf(" How many layers should this tree have?\n");
    scanf("%d\n", &layer);

for (layer = 0;layer <= layercount ; layercount++){


                star= 2*level - 1;
                space= levelcount + level - star;

            for (spacecount =0; spacecount <=spacenumber; spacecount++)
                printf(" ");

            for (starcount= 0; star < starcount; starcount++)
                printf("%c" , '*');

            for (levelcount=1; levelcount=1;levelcout++)







}







return 0;

}

推荐答案

我猜这是您想要看到的结果:

I'm guessing this is the result you want to see:

         *
        ***
       *****
      *******
     *********
    ***********
   *************
     *********
    ***********
   *************
  ***************
 *****************
*******************
        ###
        ###

这将是2个级别的6层. 因此,您首先想到的是在各个级别上使用for循环应该很好:

This would be 2 levels of 6 layers. So you're first idea to use a for-loop over the levels should be good:

        ***
       *****
      *******
     *********
    ***********
   *************

这是树的第一层. 要创建该级别的层,我们需要另一个嵌套的for循环:

This is the first level of the tree. To create the layer of this level, we'd need another, nested for-loop:

 for (level = 0;level <= levelcount ; levelcount++){
    for (layer=0; layer <= layers; layer++) {
       do_something
    }
 }

那么do_something呢?好了,这是棘手的部分,您必须计算出要在此行中打印的星星数.
为此,让我们将树的级别/层数/星数信息添加到树中:

So what about the do_something? Well here is the tricky part, you have to calculate the number of stars to be printed in this line.
For this, let's add the level/layer/# of stars information to the tree:

         *            
        ***           1/1/3
       *****          1/2/5
      *******         1/3/7
     *********        1/4/9
       *****          2/1/5
      *******         2/2/7
     *********        2/3/9
    ***********       2/4/11

对于每行,在行中添加了两个星号,我们可以推断出起始数目一定是行数的两倍.因此,对于这些行,我的第一个猜测是stars = x + [(layer - 1) * 2].通过将x设置为每层顶部的星星数,可以很容易地批准这一点.因此,对于第二层x = 5,则其第二行为5 + [(2 - 1) * 2] = 7,对于第一层3 + [(4 - 1) * 2] = 9,则为第四行.
要获得此x,我们需要知道最后一级的最后一层中的恒星数并减去4.我们也可以对此进行一些计算,但让生活更轻松,而只需记住我们使用的恒星数:

As for every line, two stars are added to the line we can infer that the number of start must be somthing with two times the number of lines. So for the lines my first guess would be stars = x + [(layer - 1) * 2]. This can easily be approved by setting x to the number of stars at the top of every layer. So for second layer x = 5 then 2nd line of this is 5 + [(2 - 1) * 2] = 7 or for 4th line in first layer 3 + [(4 - 1) * 2] = 9.
To get this x we need to know the number of stars in the last layer of the last level and substract 4. We could use some calculation for this as well, but let's make life easy and just remeber the number of stars we used:

 int x = 3;
 for (level = 0;level <= levelcount ; levelcount++){
    for (layer=0; layer <= layers; layer++) {
       print_num_of_stars(x + ((layer - 1) * 2)
    }
    x = x + ((layers - 1) * 2) - 4
 }

现在您要解决的最后一个问题是在print_num_of_stars中用所需的空格填充所有行,但这也是另一个for循环.

Now the last problem you have to solve is filling all the lines with the needed amount of spaces in print_num_of_stars, but this is just another for-loop too.

希望这会有所帮助,如果我有时弄乱了图层和级别,请原谅.

Hope this helped, please forgive if I messed up layers and levels sometimes.

这篇关于嵌套For循环,C语言中的X-mas树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆