三角形 - 题库 - 计蒜客(递归打印三角形)
三角形
查找特点,递归打印
#include <bits/stdc++.h>
using namespace std;
void show(char* buf, int w)
{
int i,j;
for(i=0; i<w; i++)
{
for(j=0; j<2*w; j++)
{
printf("%c", buf[i*2*w+j]==0? ' ' : buf[i*w*2+j]);
}
printf("\n");
}
}
void draw(char* buf, int w, int x, int y, int size)
{
if(size==2)
{
buf[(x-1)*w+y+1] = '/',buf[(x-1)*w+y+2] = '\\';
buf[x*w+y] = '/',buf[x*w+y+1] = '_',buf[x*w+y+2]
= '_',buf[x*w+y+3] ='\\';
return;
}
int n = size/2 ; //填空
draw(buf, w, x, y,n);
draw(buf, w, x-n, y+n, n);
draw(buf, w, x, y+n*2,n);
}
int main()
{
int N = 3;
while(cin>>N)
{
int t = 1;
int i;
for(i=0; i< N; i++)
t *= 2;
char* buf = (char*)malloc(t*t*2);
for(i=0; i<t*t*2; i++)
buf[i] = 0;
draw(buf, t*2, t-1, 0, t);
show(buf, t);
free(buf);
}
return 0;
}