Google-APAC2015-"New Years Eve"
来源:互联网 发布:java 命令模式 编辑:程序博客网 时间:2024/04/30 13:42
看到这么一个题目名字,突然觉得很寂寥,因为,我想到了2015的新年,估计得一个人过。
anyway,题目:New Years Eve
#1 题目理解:
看过婚礼上面的那种杯子上面放杯子么?就是类似那个模型。每个杯子下面会放着3个杯子,具体每一层的放置方式请看原题。然后假设一个人开了B瓶香槟,从最高的那个杯子倒酒,求解第L层编号为N的杯子里面容纳酒的体积。
比如test case的输入是:2 3 1,也就是开了2瓶香槟往最高的杯子倒酒,求解第3层第1个杯子的酒容量。
该test case的输出是:Case #6: 55.5555556。
#2 算法:
初始化表明第一个杯子应该装多少酒:
g[0] = B * 750
如果g[i] > 250那么就应该把多余的酒都传递到与g[i]相接触的下面三个杯子下面,假设这三个杯子的id分别是g[i],g[j],g[k](其中有一个一定是i,看规律就可以)。
所以就是g[i], g[j], g[k]都分别添加(g[i] - 250) / 3。
然后进行到下一层的处理。
至于如何计算j,k,我们能够观察出来,假设i所在的行数是row(注意,这里的row是一个level里面的三角形阵列里面的行数),那么
j = i + row - 1;
k = i + row;
#3 数据结构:
用两个一维数组,一个用来存杯子里面真实的酒体积,另外一个用来存这个杯子“如果有无限大体积”可以容纳的酒的体积。
#4 代码:
import java.util.*;import java.io.*;import java.text.DecimalFormat;public class Solution {public static void main(String[] args) {File inFile = new File("B-large-practice.in");File outFile = new File("B-large-practice.out");try {BufferedReader br = new BufferedReader(new FileReader(inFile));BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));int T = Integer.parseInt(br.readLine());for (int i = 1; i <= T; i++) {String s = br.readLine();String[] parts = s.split("\\s");int B = Integer.parseInt(parts[0]);int L = Integer.parseInt(parts[1]);int N = Integer.parseInt(parts[2]);DecimalFormat df = new DecimalFormat("#.#######"); bw.write("Case #" + i + ": " + df.format(solve(B, L, N)) + "\n");}bw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}// Calculate how much wine is in the glass// B is the number of bottles the bartender pours // L is the glass level in the pyramid// N is the number of the glass in that levelpublic static double solve(int B, int L, int N) {int NUM = ((L+1)*(L+2))/2; // add one more layer for calculation coveniencedouble[] left = new double[NUM]; // wine left in the glass[id] in level Ldouble[] pour = new double[NUM]; // wine pour in the glass[id] in level Lpour[0] = B*750;for (int level = 1; level <= L; level++) {left = pour;pour = new double[NUM];int id = 0;for (int row = 1; row <= level; row++) {for (int col = 1; col <= row; col++) {if (left[id] > 250) {double spill = (left[id] - 250) / 3;pour[id] += spill;pour[id+row] += spill;pour[id+row+1] += spill;left[id] = 250;}id++;}}}return left[N-1];}}
0 0
- Google-APAC2015-"New Years Eve"
- Google APAC test 2015 Round B Problem B - New Years Eve
- google APAC round 2 problem B New Years Eve 倒酒问题
- Google-APAC2015-Addition (1)
- Google-APAC2015-Addition (2)
- Google-APAC2015-"cut tiles"
- Google-APAC2015-"super 2048"
- Google-APAC2015-"Password Attacker"
- Google-APAC2015-"Seven-segment display"
- My new years resolution
- New Year's Eve 元旦前夜
- BNUT 1548 NEW YEARS 2014
- 1185 fudq and New Year's Eve party
- Google's Logo Celebrates 30 Years of Pac-Man
- Christmas Eve
- 12.23 Eve
- Eve Backstory
- web eve
- Objective-C中如何继承NSString等类簇
- 获取本机IP
- Flex生成随机数
- 环境变量应用时一个应注意的地方
- 调用WebService查看QQ号码状态
- Google-APAC2015-"New Years Eve"
- 项目部署到Tomat报异常:jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending ...
- 优秀程序员的一些特点
- BASE64加密解密
- 反向代理
- 队列基础
- learning OpenCV(3)
- Eeny Meeny Moo(经典Josephus变体(三大模板))
- Hibernate 的延迟加载