【问题标题】:Distribute integer value among the list values在列表值之间分配整数值
【发布时间】:2019-11-20 14:25:55
【问题描述】:

我有一个输入,我想根据输入分布使用以下逻辑修改现有列表值。

int distributeVal = 7;
List<int> validationList = new List<int>();

distributeVal 可以是任何整数,并且应该在validationList 中平均分配。 少数情况:

distributeVal               validationList               validationList(Updated)
   7                           {5,5,5}                     {5,2}
   7                           {5,6,5}                     {5,2}
   7                           {6,5,5}                     {6,1}
   8                           {2,2,2,3}                   {2,2,2,2}
   8                           {1}                         {1}    (remaining 7 ignored)
   8                           {5,2,7}                     {5,2,1}
   2                           {5,5,5}                     {2}
   3                           {1,1,5}                     {1,1,1}
   8                           {1,45,16}                   {1,7}
   0                           {1,50,50}                   {}

validationList 的分配应该基于 FCFS 的允许列表值。 我尝试这样做,但是通过根据列表值划分distributeVal然后修改它,有很多循环和条件。我怎样才能以最好的方式实现这一目标?谢谢。

【问题讨论】:

  • 如果您尝试过,您还应该提供问题中已有的代码。

标签: c# c#-4.0 logic


【解决方案1】:

你可以试试Linq来查询validationList:

  using System.Linq;

  ... 

  int distributeVal = 7;

  List<int> validationList = new List<int>() { 2, 2, 2, 3 };

  ...

  // validationList = if you want to "update" validationList
  var result = validationList
    .TakeWhile(_ => distributeVal > 0)   // Keep on while we have a value to distribute
    .Select(item => {                    // Distribution
      int newItem = item > distributeVal // two cases: 
        ? distributeVal                  //   we can distribute partialy  
        : item;                          //   or take the entire item

      distributeVal -= newItem;          // newItem has been distributed  

      return newItem;
    })
    .ToList();

 Console.Write(string.Join(", ", result));

结果:

 2, 2, 2, 1

【讨论】:

    【解决方案2】:

    这是一个非 Linq 答案,它使用直接函数来计算这些:

    private List<int> GetValidationList(int distributeVal, List<int> validationList)
    {
        List<int> outputList = new List<int>();
        int runningCount = 0;
    
        for (int i = 0; i < validationList.Count; i++)
        {
            int nextValue;
    
            if (runningCount + validationList[i] > distributeVal)
                nextValue = distributeVal - runningCount;
            else
                nextValue = validationList[i];
    
            outputList.Add(nextValue);
            runningCount += nextValue;
            if (runningCount >= distributeVal)
                break;
        }
    
        return outputList;
    }
    

    基本上,检查每个值并将其添加到输出中,如果它低于所需的总数。如果不是,请计算差异并将其添加到输出中。

    使用这些值运行:

    List<int> result;
    result = GetValidationList(7, new List<int> { 5, 5, 5 });
    result = GetValidationList(7, new List<int> { 5, 6, 5 });
    result = GetValidationList(7, new List<int> { 6, 5, 5 });
    result = GetValidationList(8, new List<int> { 2, 2, 2, 3 });
    result = GetValidationList(8, new List<int> { 1 });
    result = GetValidationList(8, new List<int> { 5, 2, 7 });
    result = GetValidationList(2, new List<int> { 5, 5, 5 });
    result = GetValidationList(3, new List<int> { 1, 1, 5 });
    result = GetValidationList(8, new List<int> { 1, 45, 16 });
    result = GetValidationList(0, new List<int> { 1, 50, 50 });
    

    给出这个输出(在List&lt;int&gt;中):

    5,2
    5,2
    6,1
    2,2,2,2
    1
    5,2,1
    2
    1,1,1
    1,7
    0
    

    【讨论】:

      【解决方案3】:

      Linq 支持的最短路径:

          private List<int> Validations(int value, List<int> validations)
          {
              List<int> outputList = new List<int>();
              int runningCount = 0;
      
              foreach (var nextValue in validations.Select(t => runningCount + t > value ? value - runningCount : t))
              {
                  outputList.Add(nextValue);
                  runningCount += nextValue;
                  if (runningCount >= value) break;
              }
      
              return outputList;
          }
      

      用法:

      var result = Validations(7, new List<int> { 1, 5, 1 });
      

      【讨论】:

        猜你喜欢
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-19
        • 1970-01-01
        • 1970-01-01
        • 2019-01-07
        • 2018-09-18
        相关资源
        最近更新 更多