现在有个问题,用Java 写一段多级科目的累加,即下级的数据累加到上级去,然后将所有的科目显示出来,是不是用递归算法?

解决方案 »

  1.   

     这是用存储过程写的 while @max_depth > 1
            begin
        insert into #ying
        select 
                                    sum(a.before_dr_amount) before_dr_amount,sum(a.before_cr_amount) before_cr_amount,
                                    sum(a.current_dr_amount) current_dr_amount,sum(a.current_cr_amount) current_cr_amount,
                                    sum(a.after_dr_amount) after_dr_amount,sum(a.after_cr_amount) after_cr_amount,
                                    sum(a.year_dr_amount) year_dr_amount,sum(a.year_cr_amount) year_cr_amount,
                                    b.parent_uuid uuid ,b.depth
        from  #ying as a,[erp.account_finance] as b
        where b.uuid = a.uuid 
          and b.depth = @max_depth 
        group by b.parent_uuid, b.depth

        set @max_depth=@max_depth-1
            end
    现在想用 Java写一段,不知道是不是用递归方法
      

  2.   

    的确可以用递归的算法,不过不用也是可以的,你可以在sq存储过程中就算好.不在java中算也行
      

  3.   

    public class test{  map<id,data> result;
      
      public data search(int root){
         int data;
         list list=get(root);
         
         if(list.size()>0){        for(object id:list){
              data+=search(id);
             }
           }else{
             result.put(id,data);
           }
           return data;
          
         }}
      

  4.   

    hashTree   看下 api  使用这个