代码之家  ›  专栏  ›  技术社区  ›  MadBoy

计数问题C#

  •  2
  • MadBoy  · 技术社区  · 16 年前

    ArrayList 比如说156340(如果是 TransferIn Buy )等,然后我把它们去掉,像156340(当 TransferOut , Sell ). 下面的解决方案可以毫无问题地解决这个问题。我遇到的问题是,对于一些旧数据,员工输入的sum是1500,而不是500+400+100+500。我该如何更改它,以便在ArrayList中有Sell/TransferOut且没有匹配项时,它应该尝试从该ArrayList中添加多个项,并查找合并到聚合中的元素。

       ArrayList alNew = new ArrayList();
       ArrayList alNewPoIle = new ArrayList();
       ArrayList alNewCo = new ArrayList();
       string tempAkcjeCzynnosc = (string) alInstrumentCzynnoscBezNumerow[i];
       string tempAkcjeInId = (string) alInstrumentNazwaBezNumerow[i];
       decimal varAkcjeCena = (decimal) alInstrumentCenaBezNumerow[i];
       decimal varAkcjeIlosc = (decimal) alInstrumentIloscBezNumerow[i];
       int index;
       switch (tempAkcjeCzynnosc) {                  
    
              case "Sell":
              case "TransferOut":
              index = alNew.IndexOf(varAkcjeIlosc);
              if (index != -1) {
                  alNew.RemoveAt(index);
                  alNewPoIle.RemoveAt(index);
                  alNewCo.RemoveAt(index);
              } else {
                  // Number without match encountred
              }
              break;
    
              case "Buy":
              case "TransferIn":
                   alNew.Add(varAkcjeIlosc);
                   alNewPoIle.Add(varAkcjeCena);
                   alNewCo.Add(tempAkcjeInId);
                   break;
        }
    }
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    这是背包问题的一个变体,称为 subset sum problem . 检查我的答案 here 用于多种解决方案。如果使用动态规划方法,要获得需要删除的实际项,只需保留第二个数组,该数组告诉您为获得某个和而添加的最后一个元素是什么,然后您就可以使用该数组来找到解决方案。如果你不能让它工作,请发回。如果你有很多数字,我还是建议使用随机化算法,它不仅更容易实现,而且更节省内存和时间(通常)。

        2
  •  4
  •   LukeH    16 年前
    推荐文章