看图写树Uva-10562

2015-01-22 20:52:44 · 作者: · 浏览: 8

题意大概:

将多叉树转化为括号表达式。

每个节点除了“-”、“|”、和空格以外的其它字符表示。

每个非叶节点的正下方总会有一个“|”字符,然后下方是一排“-”字符,恰好覆盖所有的子节点的上方。

单独的一行“#”为数据结束标记。


代码如下:

#include
  
   
#include
   
     #include
    
      using namespace std; const int maxn=100; char buf[maxn][maxn]; int num,cnt=0; void action(int row,int col) { if(buf[row+1][col]=='|'&&row+1
     
      =0) col--;//搜寻最左边的'-',然后记录! for(int i=col; buf[row+2][i]=='-'&&buf[row+3][i]!='\0'; i++) { if(buf[row+3][i]!=' ') { cout<
      
       >num; getchar(); while(num--) { cnt=0; memset(buf,'\0',sizeof(buf)); while(gets(buf[cnt])!=NULL) { cnt++; if(strcmp(buf[cnt-1],"#")==0) break; } fun(); } return 0; } /* 2 A | ------------- B C D | | ----- - E F G # (A(B()C(E()F())D(G()))) e | ----- f g # (e(f()g())) Process returned 0 (0x0) execution time : 5.005 s Press any key to continue. */