P1498 南蛮图腾

题目描述

自从到了南蛮之地,孔明不仅把孟获收拾的服服帖帖,而且还发现了不少少数民族的智慧,他发现少数民族的图腾往往有着一种分形的效果,在得到了酋长的传授后,孔明掌握了不少绘图技术,但唯独不会画他们的图腾,于是他找上了你的爷爷的爷爷的爷爷的爷爷……帮忙,作为一个好孙子的孙子的孙子的孙子……你能做到吗?

输入格式

每个数据一个数字,表示图腾的大小(此大小非彼大小) n<=10

输出格式

这个大小的图腾

输入输出样例

输入 #1
2
输出 #1
   /\
  /__\
 /\  /\
/__\/__\
输入 #2
3
输出 #2
       /\
      /__\
     /\  /\
    /__\/__\
   /\      /\
  /__\    /__\
 /\  /\  /\  /\
/__\/__\/__\/__\

 

递归! 与归纳法相似,由1到n

#include<bits/stdc++.h>
using namespace std;
string g[2048];
void graph(int n){
    int p;
   if(n==1){
       g[1]=" /\\ ";
       g[2]="/__\\";
   }
    else{
        graph(n-1);
        p=pow(2,n-1);
        for(int i=p+1; i<=2*p; i++){
            g[i]=g[i-p]+g[i-p];
        }
        for(int i=1; i<=p; i++){
            g[i].insert(0,p,' ');
            g[i].insert(g[i].length(),p,' ');
        }
    }
}
void solve(){
    int n;
    cin>>n;
    int p; p=pow(2,n);
    graph(n);
   for(int i=1; i<=p; i++)
       cout<<g[i]<<endl;
}
int main(){
	solve();
	return 0;
}

 

 

posted @ 2022-04-18 16:17  -Serein  阅读(47)  评论(0)    收藏  举报