好的,这里有一个小问题,我希望能得到一些帮助。
我有一个视图,视口大小将根据用户屏幕分辨率的不同而变化。视口需要包含N个长方体,这些长方体从右到左排列在一起,并占据视口中的所有水平空间。现在,如果所有框的大小相同,这将很容易,只需将视口宽度除以N,您就可以离开。
问题是每个框需要比左边的框小10%,因此例如,如果视口是271像素宽,并且有三个框,我将返回100,90,81
所以我需要一个算法,当处理时,视口的宽度和水平框的数量将返回一个包含每个框的宽度的数组,以便填充视口的宽度并将每个框的大小减少10%。
任何面向对象语言的答案都是很酷的。我只是想得到一些关于如何处理这个问题的想法,也许看看谁能拿出最优雅的解决方案。
致以敬意,
克里斯
发布于 2009-01-26 13:15:34
使用一个简单的几何级数,在Python中,
def box_sizes(width, num_boxes) :
first_box = width / (10 * (1 - 0.9**n))
return [first_box * 0.9**i for i in range(n)]
>>> box_sizes(100, 5)
[24.419428096993972, 21.977485287294574, 19.779736758565118, 17.801763082708607, 16.021586774437747]
>>> sum(_)
100.00000000000001您可能想要整理精度,或转换为整数,但这应该可以做到。
发布于 2009-01-26 13:11:33
Let:
x = size of the first box
n = number of boxes
P = number of pixels
n = 1: x = P
n = 2: x + .9x = P
n = 3: x + .9x + .81x = P
P = x sum[1..n](.9 ^ (n - 1))
Therefore:
x = P / sum[1..n](.9 ^ (n - 1))
Using the Geometric Progression formula:
x = P (.9 - 1) / ((.9 ^ n) - 1))
Test:
P = 100
n = 3
Gives:
x = 36.9发布于 2009-01-26 13:12:55
public static int[] GetWidths(int width, int partsCount)
{
double q = 0.9;
int[] result = new int[partsCount];
double a = (width * (1 - q)) / (1 - Math.Pow(q, partsCount));
result[0] = (int) Math.Round(a);
int sum = result[0];
for (int i = 1; i < partsCount - 1; i++)
{
result[i] = (int) Math.Round( a * Math.Pow(q, i) );
sum += result[i];
}
result[partsCount - 1] = width - sum;
return result;
}这是因为它是geometric progression。
https://stackoverflow.com/questions/479692
复制相似问题