2013-03-18 36 views
-3

在比賽中,您使用以下策略下注。每當你輸掉一個賭注,你就會把下一輪的賭注加倍。只要你贏了,下一輪的賭注就是1美元。您通過投注一美元開始本輪比賽。C增量拼圖#

例如,如果您以20美元開始,並且您在第一輪中贏了下注,則在接下來的兩輪中輸掉下注,然後在第四輪中贏下注,您將以20 + 1結束-1-2 + 4 = 22美元。

您預計完成的功能,getFinalAmount,這需要兩個參數:

  1. 第一個參數是一個整數initialAmount這是最初的錢,我們達到我們有當我們開始投注。
  2. 第二個參數是一個字符串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 
+1

爲什麼'Java'標記爲'C#'的問題嗎? – SudoRahul 2013-03-18 10:33:51

+3

這是功課還是面試問題? – TalentTuner 2013-03-18 10:36:02

+0

@沙拉布,面試作業:) – Billa 2013-03-18 10:36:29

回答

1

這是R.B.給出了正確的答案,但不知道他爲什麼刪除。

 var amountInHand = 15; 

     var possiblities = "LLLWLLLL"; 
     var lastBet = 1; 



     foreach (char c in possiblities) 
     { 
      if (c == 'W') 
      { 
       amountInHand = amountInHand + lastBet; 
       lastBet = 1; 
      } 
      else if (c == 'L') 
      { 
       amountInHand = amountInHand - lastBet; 
       lastBet = lastBet * 2; 
      } 
      //handle running out of money 
      if (lastBet > amountInHand) return amountInHand; 
     } 
+0

啊,但是如果/當你用完了錢,它會退出嗎? :P – SeKa 2013-03-18 11:32:38

+0

以上答案不正確。我今天晚上將把這個問題作爲博客飼料重新提出。 – TheSoftwareJedi 2014-04-23 21:41:14

+0

爲什麼你說@Billa是錯的?你能解釋什麼是錯的嗎?如果資金結束,我看到回覆者沒有包括這個條件。還要別的嗎? – 2014-04-24 06:22:06

0

我做了blog postcode golf這個問題。我的答案是這樣的,實際上它的工作原理...:

return possibilities.Aggregate(new{b=1,c=amountInHand,x=1},(l,o)=>l.x<0?l:o=='W'?new{b=1,c=l.c+l.b,x=1}:new{b=l.b*2,c=l.c-l.b,x=(l.c-l.b)-(l.b*2)}).c; 

取消golfed:

private static int getFinalAmount(string q, int w) 
{ 
    return possibilities.Aggregate(new { b = 1, c = amountInHand, x = false }, //initial seed gets the flag for cancel set to false 
        (l, o) => 
     l.x  //if our cancel flag is set, 
      ? l  //just return the same result 
      : o == 'W' //if the outcome was a win 
         //do the math and now also set a cancel flag to false (if we won, we can make our next bet for sure) 
       ? new { b = 1, c = l.c + l.b, x = false } 
         //do our math again, but this time the cancel flag is tricky. 
       : new { b = l.b * 2, c = l.c - l.b, 
           //we cancel if our new amount will be less than our new bet. 
           //Note, we can't use the new values that we just set in the same section - 
           //they're not available yet so we have duplicate math here. 
          x = (l.c - l.b) < (l.b * 2) }) 
        .c; //all the function returns is the current amount 
}