在比賽中,您使用以下策略下注。每當你輸掉一個賭注,你就會把下一輪的賭注加倍。只要你贏了,下一輪的賭注就是1美元。您通過投注一美元開始本輪比賽。C增量拼圖#
例如,如果您以20美元開始,並且您在第一輪中贏了下注,則在接下來的兩輪中輸掉下注,然後在第四輪中贏下注,您將以20 + 1結束-1-2 + 4 = 22美元。
您預計完成的功能,getFinalAmount
,這需要兩個參數:
- 第一個參數是一個整數
initialAmount
這是最初的錢,我們達到我們有當我們開始投注。 - 第二個參數是一個字符串
betResults
。結果的第i個字符將是'W'(贏)或'L'(輸),表示第i輪的結果。 你的功能應該返回你在所有的回合後都會有的金額。
如果在某個時候您的帳戶中沒有足夠的資金來支付投注的價值,您必須停止並返還您在該時間點的總和。
我試過這個代碼和失敗:
var amountInHand = 15;
var possiblities = "LLLWLLLL";
static int Calculate(int amountInHand, string possibles)
{
var lastBet = 1;
foreach (char c in possiblities)
{
if (c == 'W')
{
amountInHand = amountInHand + 1;
}
else if (c == 'L')
{
var loss = 0;
if (lastBet == 1)
{
loss = lastBet;
}
else if (lastBet > 1)
{
loss = lastBet * 2;
}
amountInHand = amountInHand - loss;
lastBet++;
}
}
return amountInHand;
}
預計輸出
1st round - Loss: 15-1 = 14
2nd round - Loss: 14-2 = 12 (Bet doubles)
3rd round - Loss: 12-4 = 8
4th round - Win: 8 + 8 = 16
5th round - Loss:16-1 = 15 (Since the previous bet was a win, this bet has a value of 1 dollar)
6th round - Loss: 15-2 = 13
7th round - Loss: 13-4 = 9
8th round - Loss: 9-8 = 1
爲什麼'Java'標記爲'C#'的問題嗎? – SudoRahul 2013-03-18 10:33:51
這是功課還是面試問題? – TalentTuner 2013-03-18 10:36:02
@沙拉布,面試作業:) – Billa 2013-03-18 10:36:29