UVA10562 - Undraw the Trees
来源:互联网 发布:有什么好听的网络红歌 编辑:程序博客网 时间:2024/06/05 09:51
又是递归函数的简单应用。思路较简单,
卡到我的地方是我定义的二维数组来读取数据,但是开始的时候我认为每行宽度都相同,后来我才发现每行的宽度不一定相同,我便定义了一个数组,用来记录每行的宽度,此题编过。
代码如下:
#include <iostream>#include <cstring>using namespace std;char ch[250][250];int deep, w[250];//记录各行的宽度void go(int k, int i, int j){ int left, right; cout<<"("; for(int h = i; h <= j&&h<w[k]; h++) { if(ch[k][h]!=' ') { cout<<ch[k][h]; if(k == deep-1||ch[k+1][h] != '|')cout<<"()"; else { left = right = h; while(ch[k+2][left] == '-'&&left)left--; while(ch[k+2][right] == '-'&&right != w[k+2]-1)right++; go(k+3, (left>0?left+1:0), (right<w[k+2]-1?right-1:w[k+2]-1)); } } } cout<<")";}int main (){ int n, i, j; char temp; cin>>n,cin.ignore(); while(n--) { memset(ch, 0, sizeof(ch)); memset(w,0,sizeof(w)); i = 0, j = 0; while((temp = cin.get())&&temp != '#') { if(temp == '\n') { w[j] = i; i = 0; j++; } else ch[j][i++] = temp; } cin.ignore();//注意每次输入把回车给考虑进去 deep = j; go(0,0,w[0]-1); cout<<endl; } return 0;}
- UVA10562 - Undraw the Trees
- UVA10562- Undraw the Trees
- Undraw the Trees UVA10562
- uva10562 Undraw the Trees
- uva10562 Undraw the Trees
- uva10562 -- Undraw the Trees
- Uva10562——Undraw the Trees
- uva10562 Undraw the Trees (不定叉树重建)
- 例题6-17 UVa10562 Undraw the Trees(树)
- uva10562 Undraw-the-Trees 字符题处理 递归
- Uva10562 Undraw the Trees 【递归建树】【例题6-17】
- uva10562 - Undraw the Trees 入门经典II 第六章数据结构基础 例题6-17
- 10562 - Undraw the Trees***
- 10562 - Undraw the Trees
- UVaOJ10562---Undraw the Trees
- 10562 - Undraw the Trees
- 10562 - Undraw the Trees
- 10562 - Undraw the Trees
- Java使用总结
- RatingBar详解
- 调用WebService服务客户端代码编写
- c语言内存对齐的学习
- Java中的面向对象原理(续)(11.02)
- UVA10562 - Undraw the Trees
- 纯javascript版天气预报
- metinfo 5.0图片水印bug修改和支持透明背景png水印功能的增加
- 找出mysql 中谁持有表锁
- Struts2接收checkbox的值
- Oracle SCN headroom、ORA-19706 和_external_scn_rejection_threshold_hours 参数 说明
- <script>alert(1)</script>
- Win7+Ubuntu11.10(EasyBCD硬盘安装)
- 浅谈Cloud Foundry中cloud_controller的postgres数据库