题目描述
没啥用跳过!
题目思路
递推
根据观察我们可以看出图腾的生成方法,先向右复制,在向上复制
如图:
/\
/__\
/\ -> /\ /\ -> /\ /\
/__\ /__\/__\ /__\/__\
这样我们就只用找到复制的左下角就可以了
用fu_x , fu_y 来存储左下角的坐标(二维平面需要两个坐标确定位置)
用height ,lon 来存储图形的大小
用ap来存储图形
还需要最初的图形
ap[2][1] = ' ', ap[2][2] = '/', ap[2][3] = '\\', ap[2][4] = ' ';
ap[1][1] = '/', ap[1][2] = '_', ap[1][3] = '_', ap[1][4] = '\\';
注意 :'\\' 才是 c++ 中的 \
再找 fu_x , fu_y 和 height ,lon 的关系发现 :
1.向左复制时 fu_x = 1 , fu_y = lon + 1
2.向上复制时 fu_x = height + 1 ,fu_y = lon / 2 + 1
fu_x = 1;
fu_y = lon;
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= lon; j++) {
ap[fu_x + i - 1][fu_y + j] = ap[i][j];
}
}
fu_x = height;
fu_y = lon / 2;
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= lon; j++) {
ap[fu_x + i][fu_y + j] = ap[i][j];
}
}
以上的 i , j从1开始
关系就要改:
1.向左复制时 fu_x = 1 - 1, fu_y = lon
2.向上复制时 fu_x = height,fu_y = lon / 2
height *= 2;
lon *= 2;//别忘了改变图形大小
这时就会发现当n = 2时,输出是这样的
/\
/__\
/\ /\
/__\/__\
这显然不符合要求
可以用一个省事的方法,把ap全部改为‘ ’就行
for (int i = 1; i <= 1024; i++) {
for (int j = 1; j <= 2048; j++) {
ap[i][j] = ' ';
}
}
最后输出
for (int i = height; i > 0; i--) {
for (int j = 1; j <= lon; j++) {
cout << ap[i][j];
}
cout << '\n';
}
注意,需要倒着一行一行输出
完整代码
#include<bits/stdc++.h>
using namespace std;
char ap[1024 + 1][2048 + 1];
int main() {
for (int i = 1; i <= 1024; i++) {
for (int j = 1; j <= 2048; j++) {
ap[i][j] = ' ';
}
}
ap[2][1] = ' ', ap[2][2] = '/', ap[2][3] = '\\', ap[2][4] = ' ';
ap[1][1] = '/', ap[1][2] = '_', ap[1][3] = '_', ap[1][4] = '\\';
int n;
cin >> n;
int height = 2, lon = 4;
int fu_x = 1 , fu_y = 1;
while (n != 1) {
n--;//复制n - 1轮
fu_x = 1;//复制部分
fu_y = lon;
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= lon; j++) {
ap[fu_x + i - 1][fu_y + j] = ap[i][j];
}
}
fu_x = height;
fu_y = lon / 2;
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= lon; j++) {
ap[fu_x + i][fu_y + j] = ap[i][j];
}
}
height *= 2;
lon *= 2;//别忘了改变图形大小
}
for (int i = height; i > 0; i--) {
for (int j = 1; j <= lon; j++) {
cout << ap[i][j];
}
cout << '\n';
}
return 0;
}
bye~~