C# 计数问题

发布于 2024-08-25 08:53:40 字数 1374 浏览 2 评论 0原文

我有点问题。我向 ArrayList 添加数字,例如 156、340(当它是 TransferInBuy 时)等,然后像 156 一样删除它们, 340(当为 TransferOutSell 时)。以下解决方案适用于此,没有问题。我遇到的问题是,对于一些旧数据,员工输入的总和是 1500,而不是 500+400+100+500。我将如何更改它,以便当存在 Sell/TransferOut 并且 ArrayList 内没有匹配项时,它应该尝试从该 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;
    }
}

I've a bit of a problem. I'm adding numbers to ArrayList like 156, 340 (when it is TransferIn or Buy) etc and then i remove them doing it like 156, 340 (when it's TransferOut, Sell). Following solution works for that without a problem. The problem I have is that for some old data employees were entering sum's like 1500 instead of 500+400+100+500. How would I change it so that when there's Sell/TransferOut and there's no match inside ArrayList it should try to add multiple items from that ArrayList and find elements that combine into aggregate.

   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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

獨角戲 2024-09-01 08:53:40

这是背包问题的变体,称为子集和问题。检查我的答案此处了解多种解决方案。如果使用动态编程方法,要获取需要删除的实际项目,只需保留第二个数组,它告诉您为获得特定总和而添加的最后一个元素是什么,然后您可以使用它来找到解决方案。如果您无法使其正常工作,请回复。如果你有很多数字,我建议无论如何使用随机算法,它既更容易实现,也更节省内存和时间效率(通常)。

This is a variation of the knapsack problem called the subset sum problem. Check my answer here for multiple solutions. To get the actual items you need to remove if you use the dynamic programming approach, just keep a second array that tells you what was the last element you added to get a certain sum, then you can use that to find the solution. Post back if you can't get it working. If you have a lot of numbers, I suggest the randomized algorithm anyway, it is both easier to implement and more memory and time-efficient (usually).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文