首页 > 编程语言 > 详细

Undraw the Trees 字符串数组递归画树

时间:2015-05-08 21:50:07      阅读:192      评论:0      收藏:0      [点我收藏+]

                        Undraw the Trees

题目抽象:给出一个二维字符数组表示的树,将其表示成一维字符数组表示的树。

思路:直接在二维字符数组中递归画树即可。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 const int MS=205;
 8 
 9 char str[MS][MS];
10 int n;
11 void draw(int r,int c)
12 {
13       printf("%c(",str[r][c]);
14       if(r+1<n&&str[r+1][c]==|)
15       {
16             int j=c;
17             while(j-1>=0&&str[r+2][j-1]==-)
18                   j--;
19             for(int i=j;str[r+2][i]==-&&str[r+3][i]!=\n;i++)    
20             {                                             //  这里需要特别注意   str[r+3][i]!=‘\n‘
21                   if(str[r+3][i]!= )          //   考虑这种情况str[r+2][i]==‘-‘&&str[r+3][i]==‘\n‘
22                   {
23                         draw(r+3,i);
24                   }
25             }
26       }
27       printf(")");
28 }
29 
30 int main()
31 {
32       int T;
33       scanf("%d",&T);
34       getchar();     //  fgets会读入换行符
35       while(T--)
36       {
37             n=0;
38             while(1)
39             {
40                   fgets(str[n],MS,stdin);
41                   if(str[n][0]==#)
42                         break;
43                   n++;
44             }
45             printf("(");
46             if(n)
47             {
48                   for(int i=0;str[0][i]!=\n;i++)
49                         if(str[0][i]!= )
50                   {
51                         draw(0,i);
52                         break;
53                   }
54             }
55             printf(")\n");
56       }
57       return 0;
58 }

 

Undraw the Trees 字符串数组递归画树

原文:http://www.cnblogs.com/hutaishi/p/4488763.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!