亚马逊中国 算法笔试~!

ro_nice 2013-06-05 09:41:03
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
...全文
1177 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
wasdasdfqwer 2013-06-27
  • 打赏
  • 举报
回复
引用 6 楼 AA5279AA 的回复:
对我来说最难的在于是英文说明,不是算法。 现在学技术用的都是英文文档,看的好累。 啥时候中国能开发出来汉语的文档让外国人来学中国的技术呢?
漫长的过程把
ro_nice 2013-06-26
  • 打赏
  • 举报
回复
引用 2 楼 sjlzcj 的回复:
1<<j 应该是 1<<i 吧 有笔误哦
j 如过用i的话,不对!i是你输入的长度,比如:WWLLWWLWLW 他的长度是10 那么就会发现,他没次加加后你的输赢结果不正确
shenle890303 2013-06-26
  • 打赏
  • 举报
回复
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; }
Miracle_lucy 2013-06-14
  • 打赏
  • 举报
回复
哇塞 英文的 过来围观一下
NightHary 2013-06-14
  • 打赏
  • 举报
回复
看着英文文档就头大了,啊啊啊啊。。。
xxjxxmxhlr 2013-06-14
  • 打赏
  • 举报
回复
不错,mark一下
深山老叔 2013-06-14
  • 打赏
  • 举报
回复
擦,居然把英文都看下来了,而且还看明白了....
Intboy 2013-06-08
  • 打赏
  • 举报
回复
看着 英文就头大……
shine333 2013-06-08
  • 打赏
  • 举报
回复
引用 6 楼 AA5279AA 的回复:
对我来说最难的在于是英文说明,不是算法。 现在学技术用的都是英文文档,看的好累。 啥时候中国能开发出来汉语的文档让外国人来学中国的技术呢?
不说你有生之年吧,就说你还在工作,就没这个可能——当然,你原文里面的外国人说的是斯瓦西里语什么的除外
坚持2012 2013-06-08
  • 打赏
  • 举报
回复
分享是好事,要是翻译一下就好了,呵呵,英文没法搞,哎!
l_9style 2013-06-08
  • 打赏
  • 举报
回复
感谢楼主分享!!!
oh_Maxy 2013-06-07
  • 打赏
  • 举报
回复
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做些校验。
编程点滴 2013-06-07
  • 打赏
  • 举报
回复
楼上正解
ziweixinghello 2013-06-06
  • 打赏
  • 举报
回复
引用 1 楼 robotta 的回复:
很悲催的是,我开始没写出来!


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;
	}
}


楼上正解
-AJ- 2013-06-06
  • 打赏
  • 举报
回复
分享是好事。
头号大宝贝 2013-06-06
  • 打赏
  • 举报
回复
同6楼。
lalashuai666 2013-06-06
  • 打赏
  • 举报
回复
我错了...是笔试额...真恶心
lalashuai666 2013-06-06
  • 打赏
  • 举报
回复
作完后没有测试吗?
lalashuai666 2013-06-06
  • 打赏
  • 举报
回复
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;
    }

}
失落夏天 2013-06-06
  • 打赏
  • 举报
回复
对我来说最难的在于是英文说明,不是算法。 现在学技术用的都是英文文档,看的好累。 啥时候中国能开发出来汉语的文档让外国人来学中国的技术呢?
加载更多回复(5)

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧