我理解递归的基础知识,但是当我遇到来自hackerrank的this one之类的问题时。我很快就对如何接近它感到困惑。
基本上你必须绘制一个分形ascii树(由字母Y组成),每一层向下一半Ys的数量。我似乎无法绕过基本步骤,所以我可以将它推广到其他层,如下所示:
____________________________________________________________________________________________________
__________________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__________________________________________________
如果有人能够向我推进正确的方向,我们将不胜感激。
答案 0 :(得分:4)
图像的结构如下:
Y01
Y11
---------
Y02
Y12
---------
Y04
Y14
---------
Y08
Y18
---------
Y016
Y116
Y0x&字母Y:Y0x的Y1x是分支,Y1x是截断; x是形成字母
所需的行数E.g。 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
你只需要实现调用drawY的第二个函数drawPicture(...)。如果需要,可以修改drawY。