zoukankan      html  css  js  c++  java
  • C#递归累计到父行

    搞了半天 写了一个算法,希望能帮到需要的朋友

    效果如下

    水电费用是由 就是部门水费和电费累加的,而部门水费由科室水费累加起来的

    表结构

    DataTable dt = new DataTable();
    dt.Columns.Add("pid");
    dt.Columns.Add("fatherid");
    dt.Columns.Add("Categories");
    dt.Columns.Add("Month");

    思路:
    用递归算法:累计下级数量,递归累计到上级

     public DataTable SumNumber(DataTable dt, string fatherid)
            {
                //月累计
                double m_count = 0;
    
                //查询是否有下级
                DataRow[] rows = dt.Select("fatherid='" + fatherid + "'");
    
                //上级变量,下级汇总数量到上级
                DataRow fatherow = null;
                if (dt.Select("pid='" + fatherid + "'").Length > 0)
                {
                    fatherow = dt.Select("pid='" + fatherid + "'")[0];
                }
    
                //循环子行
                foreach (DataRow row in rows)
                {
                    m_count = Convert.ToDouble(row["Month"]);
                    //判断是否还有下下级
                    DataRow[] childrows = dt.Select("fatherid='" + row["pid"] + "'");
                    foreach (DataRow item in childrows)
                    {
                        SumNumber(dt, item["pid"].ToString());
                        m_count += Convert.ToDouble(item["Month"]);
                    }
                    //累计下下级到本级
                    row["Month"] = m_count;
                    //累计本级到上级
                    if (fatherow != null)
                    {
                        fatherow["Month"] = Convert.ToDouble(fatherow["Month"]) + m_count;
                    }
                }
    
                return dt;
            }

    调用
     dt = SumNumber(dt, "0");

  • 相关阅读:
    2019 icpc西安邀请赛 点分治
    2019ccpc 秦皇岛
    hdu 5354 树上点分治
    cf 632E FFT+快速幂
    hdu 4812 树分治+逆元+手写hashmap
    2019 上海网络赛G 手写哈希map+字符串hash
    2019 上海icpc网络赛 C FFT优化卷积+小范围暴力
    hdu 6198 杜教BM
    洛谷P3804 后缀自动机
    集合总结
  • 原文地址:https://www.cnblogs.com/linyijia/p/4013605.html
Copyright © 2011-2022 走看看