题意:
给出一个二维字符数组,它代表了一棵树。然后将这棵树转化为括号表示法(以递归的形式)。
分析:
这道题最大的特色就是对数据的处理方式,里面用到了一个 fgets() 函数,这个函数的功能有点像c++里面的cin.getline()
函数介绍:
从文件结构体指针stream中读取数据,每次读取一行。读取的数据保存在buf指向的字符数组中,每次最多读取bufsize-1个字符(第bufsize个字符赋' '),如果文件中的该行,不足bufsize个字符,则读完该行就结束。如若该行(包括最后一个换行符)的字符数超过bufsize-1,则fgets只返回一个不完整的行
学习一下对输入数据的细节处理。
1 //#define LOCAL 2 #include <cctype> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 const int maxn = 200 + 10; 8 int n; 9 char buf[maxn][maxn]; 10 11 void dfs(int r, int c) 12 { 13 printf("%c(", buf[r][c]); 14 if(r+1 < n && buf[r+1][c] == '|') 15 { 16 int i = c; 17 while(i-1 >= 0 && buf[r+2][i-1] == '-') i--; //找"---"的左边界 18 while(buf[r+2][i] == '-' && buf[r+3][i] != '