我有一个编码/数学问题,我需要帮助翻译成C#。这是一款扑克筹码计算器,它可以接受BuyIn,玩家人数和每种颜色的筹码总数(有x个颜色)及其价值。帮助数学/编码可能的组合组合总数 - C#
然后它显示每个人的每个可能的组合芯片等于购买。然后用户可以选择他们想要使用的芯片组分配。最好用一个简单的例子来说明。
- 买入费:$ 10
- 玩家数:1个
- 10个红筹股,$ 1值
- 10个蓝芯片,$ 2值
- 10个生芯片,$值5
所以,可能的组合是:
R/B/G
- 10/0/0
- 8/1/0
- 6/2/0
- 5/0/1
- 4/3/0
- 2/4/0
- 1/2/1
等
我花了很多时间试图在C#/ .NET中使用算法来解决这个问题。我绊倒了可变因素 - 一套中通常只有3或4种不同的芯片颜色,但可能有任何数量。如果你有多于一个的玩家,那么你必须计算到TotalChips/NumberOfPlayers。
我开始时循环了所有的芯片,然后循环从0到NumberOfChips的颜色。这几乎是我花了最后4个小时的时间......我如何编写代码来循环访问x个芯片,然后检查芯片总和的值,并将它添加到集合中,如果它等于BuyIn ?我需要从根本上改变我的方法methinks ...
任何人都可以让我在正确的轨道上如何解决这个请吗?伪码将起作用 - 感谢您的任何建议!
以下是我迄今为止的尝试 - 这是无望的(不会编译,只是一个例子,向您展示我的思维过程,迄今为止) - 最好不要看它,因为它可能会偏见你的解决方案..
private void SplitChips(List<ChipSuggestion> suggestions)
{
decimal valueRequired = (decimal)txtBuyIn.Value;
decimal checkTotal = 0;
ChipSuggestion suggestion;
//loop through each colour
foreach (Chip chip in (PagedCollectionView)gridChips.ItemsSource)
{
//for each value, loop through them all again
foreach (Chip currentChip in (PagedCollectionView)gridChips.ItemsSource)
{
//start at 0 and go all the way up
for (int i = 0; i < chip.TotalChipsInChipset; i++)
{
checkTotal = currentChip.ChipValue * i;
//if it is greater than than ignore and stop
if (checkTotal > valueRequired)
{
break;
}
else
{
//if it is equal to then this is a match
if (checkTotal == valueRequired)
{
suggestion = new ChipSuggestion();
suggestion.SuggestionName = "Suggestion";
chipRed.NumberPerPlayer = i;
suggestion.Chips.Add(chipRed);
chipBlue.NumberPerPlayer = y;
suggestion.Chips.Add(chipBlue);
chipGreen.NumberPerPlayer = 0;
suggestion.Chips.Add(chipGreen);
//add this to the Suggestion
suggestions.Add(suggestion);
break;
}
}
}
}
}
}
就这样,我明白了这个问题:你有一套不同的芯片可以使用,价值不同。你有一个目标金额($ 10你的例子),你想看看那些你可以用它来达到这一目标金额芯片的所有组合? “球员人数”与解决方案有什么关系? – 2010-07-11 14:06:24
是的,所以NumberOfPlayers会影响芯片的数量。芯片组中有X个筹码,如果你有2个玩家并且每个人使用5个红色,那么你需要5 * 2个红色。 – Rodney 2010-07-11 15:24:18