C# 计数问题
我有点问题。我向 ArrayList
添加数字,例如 156、340(当它是 TransferIn
或 Buy
时)等,然后像 156 一样删除它们, 340(当为 TransferOut
、Sell
时)。以下解决方案适用于此,没有问题。我遇到的问题是,对于一些旧数据,员工输入的总和是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能比您想象的更棘手:
This could prove trickier than you might think:
这是背包问题的变体,称为子集和问题。检查我的答案此处了解多种解决方案。如果使用动态编程方法,要获取需要删除的实际项目,只需保留第二个数组,它告诉您为获得特定总和而添加的最后一个元素是什么,然后您可以使用它来找到解决方案。如果您无法使其正常工作,请回复。如果你有很多数字,我建议无论如何使用随机算法,它既更容易实现,也更节省内存和时间效率(通常)。
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).