扶强 2019-02-25 14:57 采纳率: 100%
浏览 326
已采纳

将以下代码改成递归形式

public void Calc1(int initCount, int avgCount, int endClass)
        {
            List<TreeData> list1 = new List<TreeData>();
            TreeData data1 = new Models.TreeData() { id = 0, name = "推客", parentid = 0, value = 0 };
            List<TreeData> list2 = new List<Models.TreeData>();
            for (int b = 0; b < avgCount; b++)
            {
                TreeData data2 = new Models.TreeData() { id = 0, name = "推客", parentid = 0, value = 0 };
                List<TreeData> list3 = new List<Models.TreeData>();
                for (int c = 0; c < avgCount; c++)
                {
                    TreeData data3 = new Models.TreeData() { id = 0, name = "推客", parentid = 0, value = 0 };

                    list3.Add(data3);
                    data2.children = list3;
                }
                list2.Add(data2);
                data1.children = list2;
            }
            list1.Add(data1);
        }

TreeData 结构如下

 public class TreeData
    {
        public string name { get; set; }

        public int? value { get; set; }

        [JsonIgnore]
        public int? id { get; set; }
        [JsonIgnore]
        public int? parentid { get; set; }

        public List<TreeData> children { get; set; }
    }

最终层级是endClass

  • 写回答

1条回答 默认 最新

  • 起个昵称真费劲 2019-02-25 15:51
    关注
            public void Calc1(int initCount, int avgCount, int endClass)
            {
                List<TreeData> list1 = new List<TreeData>();
                int index = 1;
                for (int i = 0; i < initCount; i++)
                {
                    TreeData data1 = new TreeData() { id = 0, name = "推客", parentid = 0, value = 0, index = index };
                    data1.children = new TreeData().GetTreeData(avgCount, endClass, index);
                    list1.Add(data1);
                }
            }
    
            public class TreeData
            {
                //当前深度
                public int index { get; set; }
    
                public string name { get; set; }
    
                public int? value { get; set; }
    
    
                public int? id { get; set; }
    
                public int? parentid { get; set; }
    
                public List<TreeData> children { get; set; }
    
    
                public List<TreeData> GetTreeData(int avgCount, int endClass, int index)
                {
                    List<TreeData> list = new List<TreeData>();
                    index++;
                    this.index = index;
                    for (int i = 0; i < avgCount; i++)
                    {
                        TreeData data1 = new TreeData() { id = 0, name = "第" + index + "级", parentid = 0, value = 0, index = index };
                        if (this.index != endClass)
                        {
                            data1.children = new TreeData().GetTreeData(avgCount, endClass, index);
                        }
                        list.Add(data1);
                    }
    
                    return list;
                }
            }
    
    
        //调用 - by 没想好
        Calc1(2,3,7);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)