3

我了解递归的基础知识,但是当我遇到来自hackerrank 的问题 。我很快就对如何处理它感到困惑。

基本上你必须画一个分形ascii树(由字母Y组成),向下的每一层将Y的数量减半。我似乎无法理解基本步骤,因此我可以将其推广到其他层,如下所示:

____________________________________________________________________________________________________
__________________1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1___________________
___________________1___1___1___1___1___1___1___1___1___1___1___1___1___1___1___1____________________
___________________1___1___1___1___1___1___1___1___1___1___1___1___1___1___1___1____________________
____________________1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____1_1_____________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
_____________________1_______1_______1_______1_______1_______1_______1_______1______________________
______________________1_____1_________1_____1_________1_____1_________1_____1_______________________
_______________________1___1___________1___1___________1___1___________1___1________________________
________________________1_1_____________1_1_____________1_1_____________1_1_________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
_________________________1_______________1_______________1_______________1__________________________
__________________________1_____________1_________________1_____________1___________________________
___________________________1___________1___________________1___________1____________________________
____________________________1_________1_____________________1_________1_____________________________
_____________________________1_______1_______________________1_______1______________________________
______________________________1_____1_________________________1_____1_______________________________
_______________________________1___1___________________________1___1________________________________
________________________________1_1_____________________________1_1_________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
_________________________________1_______________________________1__________________________________
__________________________________1_____________________________1___________________________________
___________________________________1___________________________1____________________________________
____________________________________1_________________________1_____________________________________
_____________________________________1_______________________1______________________________________
______________________________________1_____________________1_______________________________________
_______________________________________1___________________1________________________________________
________________________________________1_________________1_________________________________________
_________________________________________1_______________1__________________________________________
__________________________________________1_____________1___________________________________________
___________________________________________1___________1____________________________________________
____________________________________________1_________1_____________________________________________
_____________________________________________1_______1______________________________________________
______________________________________________1_____1_______________________________________________
_______________________________________________1___1________________________________________________
________________________________________________1_1_________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________

如果有人可以向我推动正确的方向,将不胜感激。

4

1 回答 1

4

图像的结构如下:

   Y01
   Y11
---------
   Y02
   Y12
---------
   Y04
   Y14
---------
   Y08
   Y18
---------
   Y016
   Y116

字母 Y 的 Y0x 和 Y1x:Y0x 是分支,Y1x 是截断;x 是组成字母所需的行数

例如 Y02 和 Y12 - 2 条线形成分支,2 条线形成主干

1---1
 1-1
  1
  1 

您会注意到还有一个“规则”:当您开始绘制新字母时(例如,您从 Yx2 转到 Yx3),您复制最后一行。

编辑:假设您将绘图保存在矩阵中,char P[63][100] 我将编写 2 个函数(不是最佳方法,但您可以对其进行优化) - drawY - 根据输入绘制 Y 字母 - drawPicutre - 绘制 Y 的“层”

drawY(int startPos, int stopPos, int*line, int lvl, int count)
{
   if(lvl>=count)
   {
     P[*line][(startPos+(stopPos-startPos))/2] = '1';
     P[*line + lvl][(startPos+(stopPos-startPos))/2 +count] = '1';
     P[*line + lvl][(startPos+(stopPos-startPos))/2 -count] = '1';
     (*line)--;
     drawY(startPos,stopPos,line,lvl,count+1);
   }

}

int line=63;
drawY(0,100, &line, 16, 1); // draws the first, and largest Y

你只需要实现第二个函数,drawPicture(...),它调用drawY。如果你愿意,你可以修改 drawY。

于 2013-11-25T21:34:54.363 回答