生成所有可能的数字分布在篮子之间。

huangapple go评论71阅读模式
英文:

Generate all possible distributions of numbers among baskets

问题

  • 有8个数字(即1, 2, 3, 4, 5, 6, 7, 8),以及5个篮子。

  • 如何生成数字在篮子之间的所有可能分布?我看到有关篮子里的苹果的情况,但这种情况不同:苹果 - 只有一些数量,而这里每个数字都是唯一的。

  • 我找到了Adrian Akison的Combinatorics包,但不清楚如何在我的情况下使用它...

  • 即使是关于如何使用包逻辑的建议也会有所帮助。期望输出的示例:

    • 0|0|0|0|1,2,3,4,5,6,7,8
    • 0|0|0|1|2,3,4,5,6,7,8
    • 0|0|0|2|1,3,4,5,6,7,8
    • 0|0|0|3|1,2,4,5,6,7,8
    • 0|0|0|4|1,2,3,5,6,7,8
    • 0|0|0|5|1,2,3,4,6,7,8
    • 0|0|0|6|1,2,3,4,5,7,8
英文:

There're 8 numbers (i.e. 1, 2, 3, 4, 5, 6, 7, 8,) and 5 baskets.
How can I generate all possible distributions of numbers among baskets? I saw cases about apples in basket but this case is not the same: apples - just some amount, while here each number is uniq.

I found greate package Combinatorics by Adrian Akison, but it's not clear how to use it in my case...

Even advice on how to use package logic would be helpful. Example of expected output:

  • 0|0|0|0|1,2,3,4,5,6,7,8
  • 0|0|0|1|2,3,4,5,6,7,8
  • 0|0|0|2|1,3,4,5,6,7,8
  • 0|0|0|3|1,2,4,5,6,7,8
  • 0|0|0|4|1,2,3,5,6,7,8
  • 0|0|0|5|1,2,3,4,6,7,8
  • 0|0|0|6|1,2,3,4,5,7,8

答案1

得分: 1

以下是您要翻译的代码部分:

So for each number we should come up with a basket it belongs:

So far so good we have `5^8 == 390625` possibilities. Here they are:

Let's have a look:

Output: (for each number we get the basket it belongs)

If you want to look at baskets:

Have a look:

Output: (for each basket we have its contain)
using System.Linq;

...

private static IEnumerable<int[]> SolveMe(int numbers, int baskets) {
  int[] solution = new int[numbers];

  do {
     yield return solution.ToArray();

     for (int i = solution.Length - 1; i >= 0; --i)
       if (solution[i] == baskets - 1) 
         solution[i] = 0;
       else {
         solution[i] += 1;

         break;  
       }
  }
  while (solution.Any(item => item != 0));
}
var result = string.Join(Environment.NewLine,
  SolveMe(8, 5).Select(solution => string.Join(", ", solution)));

Console.Write(result);
private static IEnumerable<string> Baskets(int numbers, int baskets) {
  List<int>[] line = Enumerable
    .Range(0, baskets)
    .Select(_ => new List<int>())
    .ToArray();

  foreach (int[] solution in SolveMe(numbers, baskets)) {
    foreach (var item in line)
      item.Clear();

    for (int i = 0; i < solution.Length; ++i)
      line[solution[i]].Add(i + 1);

    yield return string.Join(" | ", line.Select(item => string.Join(", ", item)));
  }
}
Console.Write(string Join(Environment.NewLine, Baskets(8, 5)));

希望这能帮助您。

英文:

So for each number we should come up with a basket it belongs:

   12345678 
   --------
   00000000 &lt;- all in basket #0
   00000001 &lt;- all in basket #0, except number 8 which is in basket #1
   ...
   00000004 &lt;- all in basket #0, except number 8 which is in basket #4
   00000010 &lt;- all in basket #0, except number 7 which is in basket #1
   00000011 &lt;- all in basket #0, except numbers 7 and 8 which are in basket #1
   ...
   44444444 &lt;- all in basket #4

So far so good we have 5^8 == 390625 possibilities. Here they are:

using System.Linq;

...

private static IEnumerable&lt;int[]&gt; SolveMe(int numbers, int baskets) {
  int[] solution = new int[numbers];

  do {
     yield return solution.ToArray();

     for (int i = solution.Length - 1; i &gt;= 0; --i)
       if (solution[i] == baskets - 1) 
         solution[i] = 0;
       else {
         solution[i] += 1;

         break;  
       }
  }
  while (solution.Any(item =&gt; item != 0));
}

Let's have a look:

var result = string.Join(Environment.NewLine,
  SolveMe(8, 5).Select(solution =&gt; string.Join(&quot;, &quot;, solution)));

Console.Write(result);

Output: (for each number we get the basket it belongs)

0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 1
0, 0, 0, 0, 0, 0, 0, 2
0, 0, 0, 0, 0, 0, 0, 3
0, 0, 0, 0, 0, 0, 0, 4
0, 0, 0, 0, 0, 0, 1, 0
...
1, 1, 0, 4, 1, 2, 1, 3
1, 1, 0, 4, 1, 2, 1, 4
...
4, 4, 4, 4, 4, 4, 4, 3
4, 4, 4, 4, 4, 4, 4, 4

If you want to look at baskets:

private static IEnumerable&lt;string&gt; Baskets(int numbers, int baskets) {
  List&lt;int&gt;[] line = Enumerable
    .Range(0, baskets)
    .Select(_ =&gt; new List&lt;int&gt;())
    .ToArray();

  foreach (int[] solution in SolveMe(numbers, baskets)) {
    foreach (var item in line)
      item.Clear();

    for (int i = 0; i &lt; solution.Length; ++i)
      line[solution[i]].Add(i + 1);

    yield return string.Join(&quot; | &quot;, line.Select(item =&gt; string.Join(&quot;, &quot;, item)));
  }
}

Have a look:

Console.Write(string.Join(Environment.NewLine, Baskets(8, 5)));

Output: (for each basket we have its contain)

1, 2, 3, 4, 5, 6, 7, 8 |  |  |  | 
1, 2, 3, 4, 5, 6, 7 | 8 |  |  | 
1, 2, 3, 4, 5, 6, 7 |  | 8 |  | 
1, 2, 3, 4, 5, 6, 7 |  |  | 8 | 
1, 2, 3, 4, 5, 6, 7 |  |  |  | 8
1, 2, 3, 4, 5, 6, 8 | 7 |  |  | 
1, 2, 3, 4, 5, 6 | 7, 8 |  |  | 
1, 2, 3, 4, 5, 6 | 7 | 8 |  | 
1, 2, 3, 4, 5, 6 | 7 |  | 8 | 
...
7 | 5, 6, 8 | 1, 2 |  | 3, 4
7 | 5, 6 | 1, 2, 8 |  | 3, 4
7 | 5, 6 | 1, 2 | 8 | 3, 4
...
 |  |  | 8 | 1, 2, 3, 4, 5, 6, 7
 |  |  |  | 1, 2, 3, 4, 5, 6, 7, 8

答案2

得分: 0

以下是翻译好的代码部分:

这里是答案

使用 Combinatorics.Collections;

var P2 = new Permutations<char>(inputSet, GenerateOption.WithoutRepetition);
var allP = new List<string>();
foreach (var p in P2)
{
    var line1 = string.Join(",", p);
    var arr = line1.Split("|").ToList();
    for (var i = 0; i < arr.Count; i++)
    {
        if (arr[i].EndsWith(','))
            arr[i] = arr[i][..^1];
        if (arr[i].StartsWith(','))
            arr[i] = arr[i].Substring(1);
        if (arr[i] == "")
            arr[i] = "0";
        var tmp = arr[i].Split(',').ToList();
        if (tmp.Count > 1)
        {
            var intL = tmp.Select(x => Convert.ToInt16(x)).ToList();
            intL.Sort();
            arr[i] = string.Join(",", intL);
        }
    }

    var line2 = string Join("|", arr);
    allP.Add(line2);
}
allP = allP.Distinct().ToList();

共有 390625 种变种。

希望这个翻译对你有所帮助。如果有任何问题,请随时提出。

英文:

Here the answer

using Combinatorics.Collections;

var P2 = new Permutations&lt;char&gt;(inputSet, GenerateOption.WithoutRepetition);
var allP = new List&lt;string&gt;();
foreach (var p in P2)
{
    var line1 = string.Join(&quot;,&quot;, p);
    var arr = line1.Split(&quot;|&quot;).ToList();
    for (var i = 0; i &lt; arr.Count; i++)
    {
        if (arr[i].EndsWith(&#39;,&#39;))
            arr[i] = arr[i][..^1];
        if (arr[i].StartsWith(&#39;,&#39;))
            arr[i] = arr[i].Substring(1);
        if (arr[i] == &quot;&quot;)
            arr[i] = &quot;0&quot;;
        var tmp = arr[i].Split(&#39;,&#39;).ToList();
        if (tmp.Count &gt; 1)
        {
            var intL = tmp.Select(x =&gt; Convert.ToInt16(x)).ToList();
            intL.Sort();
            arr[i] = string.Join(&quot;,&quot;, intL);
        }
    }

    var line2 = string.Join(&quot;|&quot;, arr);
    allP.Add(line2);
}
allP = allP.Distinct().ToList();

There're 390625 variants.

答案3

得分: -1

使用以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using Combinatorics.Collections;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
        int numBaskets = 5;
        var baskets = Enumerable.Repeat(0, numBaskets).ToList();

        var combinations = new Combinations<int>(numbers, numBaskets, GenerateOption.WithoutRepetition);

        foreach (var combination in combinations)
        {
            var currBaskets = new List<List<int>>(numBaskets);
            for (int i = 0; i < numBaskets; i++)
            {
                currBaskets.Add(new List<int>());
            }

            for (int i = 0; i < numBaskets; i++)
            {
                currBaskets[i].Add(combination[i]);
                baskets[i] = 1;
            }

            var remainingNumbers = numbers.Except(combination);

            foreach (var number in remainingNumbers)
            {
                var minIndex = baskets.IndexOf(baskets.Min());
                currBaskets[minIndex].Add(number);
                baskets[minIndex]++;
            }

            Console.WriteLine(string.Join("|", baskets) + "|" + string.Join(",", currBaskets.Select(b => string.Join(",", b)));
            baskets = Enumerable.Repeat(0, numBaskets).ToList();
        }
    }
}

这是你提供的C#代码的翻译。

英文:

you can try this :

using System;
using System.Collections.Generic;
using System.Linq;
using Combinatorics.Collections;

class Program
{
    static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
        int numBaskets = 5;
        var baskets = Enumerable.Repeat(0, numBaskets).ToList();

        var combinations = new Combinations&lt;int&gt;(numbers, numBaskets, GenerateOption.WithoutRepetition);

        foreach (var combination in combinations)
        {
            var currBaskets = new List&lt;List&lt;int&gt;&gt;(numBaskets);
            for (int i = 0; i &lt; numBaskets; i++)
            {
                currBaskets.Add(new List&lt;int&gt;());
            }

            for (int i = 0; i &lt; numBaskets; i++)
            {
                currBaskets[i].Add(combination[i]);
                baskets[i] = 1;
            }

            var remainingNumbers = numbers.Except(combination);

            foreach (var number in remainingNumbers)
            {
                var minIndex = baskets.IndexOf(baskets.Min());
                currBaskets[minIndex].Add(number);
                baskets[minIndex]++;
            }

            Console.WriteLine(string.Join(&quot;|&quot;, baskets) + &quot;|&quot; + string.Join(&quot;,&quot;, currBaskets.Select(b =&gt; string.Join(&quot;,&quot;, b))));
            baskets = Enumerable.Repeat(0, numBaskets).ToList();
        }
    }
}

huangapple
  • 本文由 发表于 2023年3月8日 16:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定