计数问题C# [英] Counting problem C#

查看:139
本文介绍了计数问题C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有点问题的。我添加号码的ArrayList 像156,340(当它是转铁采购等),然后我删除它们这样做像156,340(当它的 TransferOut )。以下解决方案适用于这没有问题。我的问题是,一些老员工数据进入了一笔的像1500,而不是500 + 400 + 100 + 500。我怎么会改变它,这样,当有销售/ TransferOut并没有匹配的ArrayList里面应该尝试从ArrayList中添加多个项目,并找到结合成聚集要素。

  ArrayList的alNew =新的ArrayList(); 
ArrayList的alNewPoIle =新的ArrayList();
ArrayList的alNewCo =新的ArrayList();
串tempAkcjeCzynnosc =(字符串)alInstrumentCzynnoscBezNumerow [I]
串tempAkcjeInId =(字符串)alInstrumentNazwaBezNumerow [I]
十进制varAkcjeCena =(十进制)alInstrumentCenaBezNumerow [I]
十进制varAkcjeIlosc =(十进制)alInstrumentIloscBezNumerow [I]
INT指数;
开关(tempAkcjeCzynnosc){

案卖出:
案TransferOut:
指数= alNew.IndexOf(varAkcjeIlosc);
如果(!指数= -1){
alNew.RemoveAt(索引);
alNewPoIle.RemoveAt(指数);
alNewCo.RemoveAt(指数);
}其他{
//不匹配数encountred
}
中断;

案买入:
案转铁:
alNew.Add(varAkcjeIlosc);
alNewPoIle.Add(varAkcjeCena);
alNewCo.Add(tempAkcjeInId);
中断;
}
}


解决方案

这是叫子集和问题背包问题的变化。检查我的答案的这里多种解决方案。为了得到你需要的,如果你使用动态规划方法去除实际的项目,只是不停地告诉你什么是你加入到获得一定金额的最后一个元素,那么你可以用它来找到解决方案的第二阵列。后回来,如果你不能得到它的工作。如果你有很多数字,我建议随机算法反正,它既是更容易实现和更多的内存和时间效率(通常情况下)。


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

解决方案

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).

这篇关于计数问题C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆