通过任意一整形数组输出一标准树结构

 

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());

}

 

 


作者:朱会震


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值