A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 朱婵 中级黑马   /  2014-2-19 18:18  /  797 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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
WWWWWWWW

Output #00:
20

Explanation:
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 = 20

Input #01:
15
LLLWLLLL

Output #01:
1

Explanation:
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






  1. package com.a.test;

  2. import java.util.Scanner;

  3. public class Amount {
  4.        
  5.        
  6.         public static void main(String[] args) {
  7.                 System.out.println("输入游戏输赢...!W代表赢、L表示输:如:WWWWLLLLLLW");
  8.                 Scanner scanner = new Scanner(System.in);
  9.                 String scan_input = scanner.next().toUpperCase();
  10.                 int initialAmount = 20;
  11.                 getFinalAmount(initialAmount, scan_input);
  12.                
  13.         }

  14.         protected static int getFinalAmount(int initialAmount, String betResults) {
  15.                 int j = 0;
  16.                 char ch = 'W';
  17.                 for (int i = 0; i < betResults.length(); i++) {
  18.                         if(ch == betResults.charAt(i)) {
  19.                                 if(j == 0) {
  20.                                         initialAmount = initialAmount + 1;
  21.                                 } else {
  22.                                         initialAmount = initialAmount+(1<<j);
  23.                                 }
  24.                                 j=0;
  25.                                 System.out.println("W: "+initialAmount);
  26.                         } else {
  27.                                 if(j == 0) {
  28.                                         initialAmount = initialAmount-1;
  29.                                 } else {
  30.                                         initialAmount = initialAmount-(1<<j);
  31.                                 }
  32.                                 j++;
  33.                                 System.out.println("L: "+initialAmount);
  34.                         }
  35.                        
  36.                                
  37.                 }
  38.                 return initialAmount;
  39.         }
  40. }

复制代码



评分

参与人数 1技术分 +1 收起 理由
何伟超 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马