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
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
解决方案 »
- 在 myeclipse 8.6中DB Browser怎么样连接SQL server2008
- MyEclipse安装jbpm系统提示我已经安装完成了,怎么我装上去之后在new里面看不到process project呢,以前都是有的啊
- 高分求一个JSP动态生成表格,解决马上结贴!
- Linux中运行.jar文件的问题
- 高手请帮忙 谢谢了 很急
- 奇怪的jsp调用问题!大虾帮帮忙,我郁闷一下午了!
- 奇怪的问题。。。?大哥们,帮忙看一下啊
- 问两个菜鸟问题
- 如何在界面上 连线(很难实现的哦)
- 请教各位大神一个关于网站有些用户打不开的问题
- java web项目调试后有如下错误,请大家帮看看啊
- html改成jsp 无法显示
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;
}
}
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;
}}
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;
}