我遇到了計數問題,這是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