繁体   English   中英

在列表值之间分配 integer 值

[英]Distribute integer value among the list values

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

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

DistributeVal 可以是任何 integer 并且应该在验证列表中平均分配。 少数情况:

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然后修改它,有很多循环和条件。 我怎样才能以最好的方式实现这一目标? 谢谢。

您可以尝试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

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

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

本质上,go 通过每个值并将其添加到 output 如果它低于所需的总数。 如果不是,请计算差异并将其添加到 output。

使用这些值运行:

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

给出这个 output (在List<int>中):

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM