gpt4 book ai didi

recursion - 递归绘图

转载 作者:行者123 更新时间:2023-12-04 02:39:20 25 4
gpt4 key购买 nike

我了解递归的基础知识,但是当我遇到像this one这样的问题时来自 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__________________________________________________

如果有人能在正确的方向上插入我,我将不胜感激。

最佳答案

图像的结构如下:

   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。

关于recursion - 递归绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20202332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com