通过任意一整形数组输出一标准树结构
2009-02-06 13:26
351 查看
static void Tree(int[] hs)
{
//int[] hs = { 2, 3, 4, 1, 2, 2, 3, 4, 3, 3, 4, 5, 5, 2, 4, 4 };
int k = 0;
int h;
int s = 5;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hs.Length; i++)
{
h = hs[i];
if (h < s) s = h;
if (i > 0) k = hs[i - 1];
if (h <= k)
{
sb.Remove(sb.Length - 1, 1);
sb.Append("|");
for (int a = 0; a < h - s; a++)
sb.Append("-");
}
sb.Append(h.ToString() + ", ");
}
string[] strs = sb.ToString().Split('|');
string[] sts;
int h1 = 0;
string ht = "";
sb = new StringBuilder();
for (int b = 0; b < strs.Length; b++)
{
sts = strs[b].Split(',');
for (int c = 0; c < sts.Length - 1; c++)
{
if (c == 0)
{
if (sts[c].StartsWith("-"))
{
string ht2 = "";
int idx = Math.Min(sts[c].Length - 1, ht.Split('.').Length - 1);
int d = int.Parse(ht.Split('.')[idx]) + 1;
for (int e = 0; e < idx; e++)
{
ht2 += ht.Split('.')[e] + ".";
}
ht = ht2 + d.ToString();
}
else
{
h1++;
ht = h1.ToString();
}
}
else ht += ".1";
for (int l = 0; l < ht.Split('.').Length; l++)
sb.Append("-");
sb.Append(ht);
sb.Append("/n");
}
}
Console.Write(sb.ToString());
}
{
//int[] hs = { 2, 3, 4, 1, 2, 2, 3, 4, 3, 3, 4, 5, 5, 2, 4, 4 };
int k = 0;
int h;
int s = 5;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hs.Length; i++)
{
h = hs[i];
if (h < s) s = h;
if (i > 0) k = hs[i - 1];
if (h <= k)
{
sb.Remove(sb.Length - 1, 1);
sb.Append("|");
for (int a = 0; a < h - s; a++)
sb.Append("-");
}
sb.Append(h.ToString() + ", ");
}
string[] strs = sb.ToString().Split('|');
string[] sts;
int h1 = 0;
string ht = "";
sb = new StringBuilder();
for (int b = 0; b < strs.Length; b++)
{
sts = strs[b].Split(',');
for (int c = 0; c < sts.Length - 1; c++)
{
if (c == 0)
{
if (sts[c].StartsWith("-"))
{
string ht2 = "";
int idx = Math.Min(sts[c].Length - 1, ht.Split('.').Length - 1);
int d = int.Parse(ht.Split('.')[idx]) + 1;
for (int e = 0; e < idx; e++)
{
ht2 += ht.Split('.')[e] + ".";
}
ht = ht2 + d.ToString();
}
else
{
h1++;
ht = h1.ToString();
}
}
else ht += ".1";
for (int l = 0; l < ht.Split('.').Length; l++)
sb.Append("-");
sb.Append(ht);
sb.Append("/n");
}
}
Console.Write(sb.ToString());
}
相关文章推荐
- //通过指针变量访问并输出结构数组的各元
- C++学习笔记(一)--整形,std输出,浮点数,数组,字符串,结构,指针,循环
- 在一个长度为n的整形数组a里,除了三个数字只出现一次外,其他的数字都出现了2次。请写程序输出任意一个只出现一次的数字
- 给定任意数组获取任意连续的项求和的最大值,并输出新的数组
- [学习笔记]通过open函数改变标准输出的方法
- 2017/12/31Java基础学习——数组输出の通过Arrays.toString()方法
- PHP 通过数组判断数组顺序输出是否是二叉排序树的后序遍历结果
- 【华为】题目:有两个数组a,b,大小都为n,数组元素的值任意,无序; 要求: 通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小。
- 一个H行L列的二维矩阵数组(整形),求每行元素总和,并将每行总和排序输出
- [置顶] 一位数组返回id和pid通过这两个参数转换为树形结构数据,和树形结构的渲染
- python调用外部子进程,通过管道实现异步标准输入和输出的交互
- 第十四周项目一数组大折腾(2)创建一个长度为10的整形数组并初始化,由后往前(由第9个元素向第0个元素方向)输出数组中所有元素的值,改变数组元素的值,令所有的数组加倍,输出改变后的值。
- 第十四周项目一数组大折腾(3)创建一个长度为16的整形数组a并初始化,先输出数组中元素值为3的倍数的数,再输出所有下标为3的倍数的元素值
- 通过json_decode转出来的数组转换成标准的array
- 开辟一个空房间,随机生成一个有30个数的整形数组,找出其中的重复数字,输出其余数字
- 重定向已经运行进程的标准输出到文件的办法(通过ptrace注入代码到其他进程并运行)
- PHP 打印输出数组内容及结构
- 编写程序通过栈的方式将任意输入的字符串内容进行逆序输出。
- 编写一个函数,从标准输入读取一列整数, 把这些值存储在一个动态分配的数组中并返回这个数组。 函数通过观察EOF判断输入列表是否结束。 数组的第一个数是数组包含的值的个数, 它的后面就是这些整数值。
- linux下通过dup2标准输出重定向查看系统进程方法(也是popen函数实现的方法)