我有一個編碼/數學問題,我需要幫助翻譯成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