洛谷1498 南蛮图腾【递归】

题目https://www.luogu.org/problemnew/show/P1498

题意:

题意就是输入一个n,输出一个n大小的三角形,但是又没说大小怎么定义。【一脸懵逼】

看了题解才搞明白n=1时就是最顶上那个小三角形

n=2就是把小三角形向下复制两个,n=3就是把n=2的向下复制两个。

思路:

感觉自己好像递归分治这些搞得挺差的。

像这道题目 因为涉及到空格挺麻烦的,我们在一开始并不知道需要多少空格。

所以比较好的做法就是我们倒着存三角形,然后倒着输出。

每次都把三角形往右复制一个,往下复制一个就可以了。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<map>
 4 #include<set>
 5 #include<iostream>
 6 #include<cstring>
 7 #include<algorithm>
 8 #include<vector>
 9 #include<cmath> 
10 #include<queue>
11 
12 #define inf 0x7f7f7f7f
13 using namespace std;
14 typedef long long LL;
15 typedef pair<int, int> pr;
16 
17 int n, height, width;
18 char c[3001][3001];
19 
20 void copyright()
21 {
22     for(int i = 1; i <= height; i++){
23         for(int j = 1; j <= width; j++){
24             c[i][j + width] = c[i][j];
25         } 
26     }
27 }
28 
29 void copydown()
30 {
31     for(int i = 1; i <= height; i++){
32         for(int j = 1; j <= width; j++){
33             c[i + height][width / 2 + j] = c[i][j];
34         }
35     }
36 }
37 
38 int main()
39 {
40     scanf("%d", &n);
41     for(int i = 0; i < 3001; i++){
42         for(int j = 0; j < 3001; j++){
43             c[i][j] = ' ';
44         }
45     }
46     c[1][1] = c[2][2] = '\\';
47     c[1][2] = c[1][3] = '_';
48     c[1][4] = c[2][3] = '/';
49     height = 2;
50     width = 4;
51     for(int i = 2; i <= n; i++){
52         copyright();
53         copydown();
54         height *= 2;
55         width *= 2;
56     }
57     
58     for(int i = height; i >= 1; i--){
59         for(int j = 1; j <= width; j++){
60             if(c[i][j] == '\\')printf("/");
61             else if(c[i][j] == '/')printf("\\");
62             else printf("%c", c[i][j]);
63         }
64         printf("\n");
65     }
66     
67     
68     return 0;
69 }

 

posted @ 2019-02-12 10:37  wyboooo  阅读(415)  评论(0编辑  收藏  举报