VMware coding Challenge: Coin Toss Betting

 1     static int CoinTossEndAmount(int betAmount, String coinTossResults) {
 2         if (betAmount <=0 || coinTossResults.length() == 0) return betAmount;
 3         long Amount = betAmount;
 4         long onebet = 1;
 5         for (int i=0; i<coinTossResults.length(); i++) {
 6             if (coinTossResults.charAt(i) == ‘H‘) {
 7                 Amount += onebet;
 8                 onebet *= 2;
 9             }
10             else {
11                 Amount -= onebet;
12                 onebet /= 2;
13             }
14             if (Amount < onebet) return 0;
15             if (onebet < 1) onebet = 1;
16         }
17         return (int)Amount;
18     }
时间: 2024-10-06 04:15:30

VMware coding Challenge: Coin Toss Betting的相关文章

VMware coding Challenge

思路:这道题要观察,举个例子,1 2 * * 3 * 4 * 5 * * 6 7 * 8 * *, 用Stack,先序遍历,遇到数字就入栈,如果遇到 * *,说明栈顶节点是叶子节点,一条根到叶子的路径这时候就存在于栈之中,只要计算栈的size(),就知道当前这条路径的深度,树的height就是这些深度的最大值. 空格的引入增添了不少判断上的麻烦, 比如: ab * *, 这棵树的height是1 而不是2. 在遍历节点的时候需要注意取空格之前的所有元素一起看

VMware Coding Challenge: Removing Duplicates Entries

1 static LinkedListNode removeDuplicates(LinkedListNode list) { 2 LinkedListNode cur = list; 3 HashSet<Integer> set = new HashSet<Integer>(); 4 set.add(list.val); 5 while (cur.next != null) { 6 if (!set.contains(cur.next.val)) { 7 set.add(cur.

VMware Coding Challenge: The Heist

类似BackpackII问题 1 static int maximize_loot(int[] gold, int[] silver) { 2 int[][] res = new int[gold.length+silver.length+1][10001]; 3 res[0][0] = 0; 4 for (int i=1; i<=gold.length; i++) { 5 for (int j=0; j<=10000; j++) { 6 res[i][j] = Math.max(res[i-

VMware Coding Challenge: Possible Scores &amp;&amp; Summary: static

Combination Sum I 那道题的变体 1 /* 2 * Complete the function below. 3 */ 4 5 static int is_score_possible(int score, int[] increments) { 6 Arrays.sort(increments); 7 ArrayList<Integer> res = new ArrayList<Integer>(); 8 res.add(0); 9 helper(res, inc

VMware coding Challenge:Date of Weekday

这道题再次证明了这种细节的题目,画个图容易搞清楚 1 import java.util.Scanner; 2 3 4 public class Solution2 { 5 static int DateOfWeekday(int date, int weekday) { 6 int cur = date%7; 7 int res = 0; 8 int dif = 0; 9 if (weekday > 0) { 10 dif = 7*((weekday-1)/7) + (weekday%7-cur

poj 3440 Coin Toss(概率)

http://poj.org/problem?id=3440 大致题意:给出一个n*m的格子,每个格子的边长为t,随意抛一枚硬币并保证硬币的圆心在格子里或格子边上,硬币的直径为c,求硬币覆盖格子的个数的概率. 思路:高中的概率题,ms是几何概型.根据分别覆盖1,2,3,4个格子时圆心可分部的面积比上总面积就是答案. #include <stdio.h> #include <iostream> #include <algorithm> #include <set&g

【POJ 3440】 Coin Toss(概率公式)

[POJ 3440] Coin Toss(概率公式) Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3591   Accepted: 957 Description In a popular carnival game, a coin is tossed onto a table with an area that is covered with square tiles in a grid. The prizes ar

[ACM] POJ 3440 Coin Toss (几何概率)

Coin Toss Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3019   Accepted: 817 Description In a popular carnival game, a coin is tossed onto a table with an area that is covered with square tiles in a grid. The prizes are determined by t

POJ 3440 Coin Toss

高中概率的几何概型,这也叫作题,不过输出真的很坑. 题目大意: n*m个边长为t的正方形组成的矩形.往矩形上抛一个直径为c的硬币,问覆盖1,2,3,4个矩形的概率为多少? 解题思路: 计算出覆盖1,2,3,4个矩形时硬币圆心可以在的位置区域.就能求出概率了~ 下面是代码: #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #incl