看到这么一个题目名字,突然觉得很寂寥,因为,我想到了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 level
public static double solve(int B, int L, int N) {
int NUM = ((L+1)*(L+2))/2; // add one more layer for calculation covenience
double[] left = new double[NUM]; // wine left in the glass[id] in level L
double[] pour = new double[NUM]; // wine pour in the glass[id] in level L
pour[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];
}
}
本文探讨了一个关于香槟塔酒容量分配的数学问题,通过解析特定算法和数据结构,详细解答了如何计算给定条件下某一层指定位置杯子中的酒容量。

被折叠的 条评论
为什么被折叠?



