亚马逊中国 算法笔试~!

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
...全文
1257 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; }
无形忍者 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)
随着通信电子技术的迅速发展,信息技术给家居行业产生了深远的影响,家居环境的智能化监控已经成为智能家居的一个重要的发展方向。人们逐渐对自己的生活提出一种更高的要求,他们需要一种智能化、可交互,并且融合现代创新科技的产品来改善他们的生活环境,使他们生活更加安全、舒适、便捷、智能。本文根据智能家居的发展背景和研究现状,并且从实用性和可行性角度出发,研究设计了一种基于STM32单片机的智能家居系统。该系统由一个多功能综合的技术系统组成,各个多功能子系统间具有协同配合能力。基于STM32单片机实现的功能子系统包括:智能温度检测,智能湿度检测,智能烟雾/火灾检测智能检测,无线传输,人机交互机构,风扇调节,报警模块。 整个系统分为前端51单片机采集板和后端STM32单片机接收板。 采集板使用DHT11温湿度传感器、MQ烟雾传感器完成室内家居环境的采集。然后通过nRFL24L01将采集到的数据发送给后端。接收板使用LCD1602完成数据显示、使用蜂鸣器模块报警,使用风扇驱动模块调节室内家居环境。本文完成了智能家居控制系统前端、后端软硬件的设计,使用Altium designer绘制了电路原理图,使用Keil C完成了51单片机和STM32单片机的编程与调试。 最后,对本文设计的基于STM32的智能家居控制系统进行部署调试。试验结果表明,该系统可成功应用在智能家居环境检测调节和火灾安全防护的领域,可提高家居生活智能化水平。

81,111

社区成员

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

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