英文:
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 <- all in basket #0
00000001 <- all in basket #0, except number 8 which is in basket #1
...
00000004 <- all in basket #0, except number 8 which is in basket #4
00000010 <- all in basket #0, except number 7 which is in basket #1
00000011 <- all in basket #0, except numbers 7 and 8 which are in basket #1
...
44444444 <- all in basket #4
So far so good we have 5^8 == 390625
possibilities. Here they are:
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));
}
Let's have a look:
var result = string.Join(Environment.NewLine,
SolveMe(8, 5).Select(solution => string.Join(", ", 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<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)));
}
}
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<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();
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<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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论