In a game, you bet using the following strategy. Whenever you lose a bet, you double the value of the bet for the next round. Whenever you win, the bet for the next round will be one dollar. You start the round by betting one dollar.For example, if you start with 20 dollars, and you win the bet in the first round, lose the bet in the next two rounds and then win the bet in the fourth round, you will end up with 20+1-1-2+4 = 22 dollars.You are expected to complete the function, getFinalAmount, which takes two arguments. The first argument is an integer initialAmount which is the initial money we amount we have when we start the betting. The second argument is a string betResultsThe ith character of outcome will be either 'W' (win) or 'L' (lose), denoting the result of the ith round.
Your function should return the amount of money you will have after all the rounds are played. If at some point you don't have enough money in your account to cover the value of the bet, you must stop and return the sum you have at that point.Sample Test Cases: Input #00:
12
WWWWWWWWOutput #00:
20Explanation: 
The initial amount is 12, for every win, you gain 1 dollar.
There are totally 8 consecutive wins and no losses, hence total money gained = 12 + 8 = 20Input #01:
15
LLLWLLLLOutput #01:
1Explanation:
The initial amount is 15. As stated in the problem, the amount of bet doubles for every loss.
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算法Amazon

解决方案 »

  1.   

    很悲催的是,我开始没写出来!package com.a.test;import java.util.Scanner;public class Amount {


    public static void main(String[] args) {
    System.out.println("输入游戏输赢...!W代表赢、L表示输:如:WWWWLLLLLLW");
    Scanner scanner = new Scanner(System.in);
    String scan_input = scanner.next().toUpperCase();
    int initialAmount = 20;
    getFinalAmount(initialAmount, scan_input);

    } protected static int getFinalAmount(int initialAmount, String betResults) {
    int j = 0;
    char ch = 'W';
    for (int i = 0; i < betResults.length(); i++) {
    if(ch == betResults.charAt(i)) {
    if(j == 0) {
    initialAmount = initialAmount + 1;
    } else {
    initialAmount = initialAmount+(1<<j);
    }
    j=0;
    System.out.println("W: "+initialAmount);
    } else {
    if(j == 0) {
    initialAmount = initialAmount-1;
    } else {
    initialAmount = initialAmount-(1<<j);
    }
    j++;
    System.out.println("L: "+initialAmount);
    }


    }
    return initialAmount;
    }
    }
      

  2.   

    1<<j  应该是 1<<i 吧 有笔误哦
      

  3.   

    public class sunm {
    public static void main(String[] args) {
            System.out.println("输入游戏输赢...!W代表赢、L表示输:如:WWWWLLLLLLW");
            Scanner scanner = new Scanner(System.in);
            String scan_input = scanner.next().toUpperCase();
            int initialAmount = 20;
            getFinalAmount(initialAmount, scan_input);
             
        }
     
        protected static int getFinalAmount(int initialAmount, String betResults) {
            int j = 0;
            char ch = 'W';
            for (int i = 0; i < betResults.length(); i++) {
                if(ch == betResults.charAt(i)) {
                    if(j == 0) {
                        initialAmount = initialAmount + 1;
                    } else {
                        initialAmount = initialAmount+(1<<j);
                    }
                    if((i+1)<betResults.length()){
                    if(ch==betResults.charAt(i+1)){
                     j++;
                    }else{
                     j=0;
                    }
                    }
                    
                    System.out.println("W: "+initialAmount);
                } else {
                    if(j == 0) {
                        initialAmount = initialAmount-1;
                    } else {
                        initialAmount = initialAmount-(1<<j);
                    }
                    if((i+1)<betResults.length()){
                     if(ch==betResults.charAt(i+1)){
                     j=0;
                        }else{
                         j++;
                        }
                    }
                    System.out.println("L: "+initialAmount);
                }
                 
                     
            }
            return initialAmount;
        }}
      

  4.   

    LZ似乎少了一个判断: If at some point you don't have enough money in your account to cover the value of the bet, you must stop and return the sum you have at that point.应该在for开头处,对initialAmount做些校验。
      

  5.   

     j  如过用i的话,不对!i是你输入的长度,比如:WWLLWWLWLW 他的长度是10 那么就会发现,他没次加加后你的输赢结果不正确
      

  6.   

    public static int getFinalNum(String flag , int startNum){
    int bet = 1;
    String[] list = flag.split(",");
    for(int i = 0 ; i < list.length ; ++i){
    if(startNum >= bet){
    if("w".equalsIgnoreCase(list[i])){
    startNum += bet;
    bet = 1;
    }else{
    startNum -= bet;
    bet *= 2;
    }
    }else{
    return startNum;
    }
    }
    return startNum;
    }