我遇到了计数问题,这是this问题的延续。我不是一个真正的数学家,所以我很难找出这个建议作为解决方案的subset sum problem
。子集总和问题
我有4 ArrayList
我在其中保存数据:alId,alTransaction,alNumber,alPrice
类型|交易| Number |价格
8 |购买| 95.00000000 | 305.00000000
8 |购买| 126.00000000 | 305.00000000
8 |购买| 93.00000000 | 306.00000000
8 |转出| 221.00000000 | 305.00000000
8 |转入| 221.00000000 | 305.00000000
8 |卖| | 93.00000000 | 360.00000000
8 |卖| | 95.00000000 | 360.00000000
8 |卖| | 126.00000000 | 360.00000000
8 |购买| 276.00000000 | 380.00000000
最终我试图得到什么的留给客户,什么是离开我投入3名数组列表:
- alNewHowMuch(相当于alNumber),
- alNewPrice(相当于alPrice)
- alNewInID(corrseponds到alID)
ArrayList alNewHowMuch = new ArrayList();
ArrayList alNewPrice = new ArrayList();
ArrayList alNewInID = new ArrayList();
for (int i = 0; i < alTransaction.Count; i++) {
string transaction = (string) alTransaction[i];
string id = (string) alID[i];
decimal price = (decimal) alPrice[i];
decimal number = (decimal) alNumber[i];
switch (transaction) {
case "Transfer out":
case "Sell":
int index = alNewHowMuch.IndexOf(number);
if (index != -1) {
alNewHowMuch.RemoveAt(index);
alNewPrice.RemoveAt(index);
alNewInID.RemoveAt(index);
} else {
ArrayList alTemp = new ArrayList();
decimal sum = 0;
for (int j = 0; j < alNewHowMuch.Count; j ++) {
string tempid = (string) alNewInID[j];
decimal tempPrice = (decimal) alNewPrice[j];
decimal tempNumbers = (decimal) alNewHowMuch[j];
if (id == tempid && tempPrice == price) {
alTemp.Add(j);
sum = sum + tempNumbers;
}
}
if (sum == number) {
for (int j = alTemp.Count - 1; j >= 0; j --) {
int tempIndex = (int) alTemp[j];
alNewHowMuch.RemoveAt(tempIndex);
alNewPrice.RemoveAt(tempIndex);
alNewInID.RemoveAt(tempIndex);
}
}
}
break;
case "Transfer In":
case "Buy":
alNewHowMuch.Add(number);
alNewPrice.Add(price);
alNewInID.Add(id);
break;
}
}
基本上我添加和从阵列根据交易类型,交易ID和号码去除的东西。我将数字添加到ArrayList 156,340(当它是TransferIn或Buy)等等,然后删除它们,比如156,340(当它是TransferOut,Sell)。我的解决方案可以毫无问题地工作。我遇到的问题是,对于一些旧数据,员工正在输入1500而不是500 + 400 + 100 + 500。我将如何更改它,以便在存在Sell/TransferOut
或Buy/Transfer In
且ArrayList内没有匹配时,它应该尝试添加来自该ArrayList
的多个项目并查找组合为聚合的元素。
里面我的代码,我想,当没有比赛(指数== 1)
int index = alNewHowMuch.IndexOf(number);
if (index != -1) {
alNewHowMuch.RemoveAt(index);
alNewPrice.RemoveAt(index);
alNewInID.RemoveAt(index);
} else {
ArrayList alTemp = new ArrayList();
decimal sum = 0;
for (int j = 0; j < alNewHowMuch.Count; j ++) {
string tempid = (string) alNewInID[j];
decimal tempPrice = (decimal) alNewPrice[j];
decimal tempNumbers = (decimal) alNewHowMuch[j];
if (id == tempid && tempPrice == price) {
alTemp.Add(j);
sum = sum + tempNumbers;
}
}
if (sum == number) {
for (int j = alTemp.Count - 1; j >= 0; j --) {
int tempIndex = (int) alTemp[j];
alNewHowMuch.RemoveAt(tempIndex);
alNewPrice.RemoveAt(tempIndex);
alNewInID.RemoveAt(tempIndex);
}
}
}
但它只能在满足一定的条件,以及失败的休息,以解决简单的总结一切问题。
编辑:由于你们中的一些人对我的波兰变量名称感到惊讶(并且不知所措),所以我把它们全部翻译成英文以简化和可见性。希望这会帮助我得到一些帮助:-)
您对标识符的选择是不可思议的...... – Joren 2010-04-25 13:52:52
不好的使用开关,两个如果已经足够了.. – 2010-04-25 13:56:33
@Joren:这在波兰可能会更有意义。 – 2010-04-25 13:56:55