-3

在比赛中,您使用以下策略下注。每当您输掉一个赌注时,下一轮赌注的价值就会翻倍。每当您获胜时,下一轮的赌注将是一美元。您通过下注一美元开始这一轮。

例如,如果您以 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
4

2 回答 2

1

这是RB给出的正确答案,但不知道他为什么删除。

      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;
        }
于 2013-03-18T10:48:14.717 回答
0

我写了一篇关于这个问题的博客文章和一个代码高尔夫。我的回答是这样的,而且确实有效……:

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;

未打高尔夫球:

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
}
于 2014-04-24T13:59:44.327 回答