Algorithm 调整列大小算法
我有一组不同宽度的列,我需要一个算法来重新调整它们的大小,使它们的值y大于它们所有宽度的总和 我希望算法能够优先均衡宽度。因此,如果我有一个绝对巨大的值,那么这些列的宽度将或多或少相同。如果没有足够的空间,我希望优先考虑较小的单元格 有什么好主意吗?我更喜欢像这样简单的东西:Algorithm 调整列大小算法,algorithm,math,graphics,Algorithm,Math,Graphics,我有一组不同宽度的列,我需要一个算法来重新调整它们的大小,使它们的值y大于它们所有宽度的总和 我希望算法能够优先均衡宽度。因此,如果我有一个绝对巨大的值,那么这些列的宽度将或多或少相同。如果没有足够的空间,我希望优先考虑较小的单元格 有什么好主意吗?我更喜欢像这样简单的东西: getNewWidths(NewWidth, ColumnWidths[]) returns NewColumnWidths[] 我将分两步进行分解,第一步决定需要多少均衡(0到1之间),第二步调整它以适应新的总宽度 例
getNewWidths(NewWidth, ColumnWidths[]) returns NewColumnWidths[]
我将分两步进行分解,第一步决定需要多少均衡(0到1之间),第二步调整它以适应新的总宽度 例如在
def get_new_widths new_total, widths
max = widths.max
f = how_much_equalizing(new_total) # return value between 0.0 and 1.0
widths = widths.collect{|w| w*(1-f)+max*f}
sum = widths.inject(0){|a,b|a+b}
return widths.collect{|w| w/sum*new_total}
end
def how_much_equalizing new_total
return [1.0, (new_total / 2000.0)].min
end
我将分两步进行分解,第一步决定需要多少均衡(0到1之间),第二步调整它以适应新的总宽度 例如在
def get_new_widths new_total, widths
max = widths.max
f = how_much_equalizing(new_total) # return value between 0.0 and 1.0
widths = widths.collect{|w| w*(1-f)+max*f}
sum = widths.inject(0){|a,b|a+b}
return widths.collect{|w| w/sum*new_total}
end
def how_much_equalizing new_total
return [1.0, (new_total / 2000.0)].min
end
伪代码:
w = NewWidth
n = ColumnWidths.count
sort(ColumnWidths, ascending)
while n > 1 and ColumnWidths[n-1] > (w/n):
w = w - ColumnWidths[n-1]
n = n - 1
for i = 0 to n-1:
ColumnWidths[i] = w / n
您需要添加一些代码来重新分配w/n计算中的任何舍入值,但我认为这样做就可以了。伪代码:
w = NewWidth
n = ColumnWidths.count
sort(ColumnWidths, ascending)
while n > 1 and ColumnWidths[n-1] > (w/n):
w = w - ColumnWidths[n-1]
n = n - 1
for i = 0 to n-1:
ColumnWidths[i] = w / n
您需要添加一些代码来重新分配w/n计算中的任何舍入值,但我认为这样做就可以了。Mark Ransom的回答给出了正确的算法,但如果您在确定发生了什么问题时遇到困难,这里有一个实际的Python实现:
def getNewWidths(newWidth, columnWidths):
# First, find out how many columns we can equalize
# without shrinking any columns.
w = newWidth
n = len(columnWidths)
sortedWidths = sorted(columnWidths) # A sorted copy of the array.
while sortedWidths[n - 1] * n > w:
w -= sortedWidths[n - 1]
n -= 1
# We can equalize the n narrowest columns. What is their new width?
minWidth = w // n # integer division
sparePixels = w % n # integer remainder: w == minWidth*n + sparePixels
# Now produce the new array of column widths.
cw = columnWidths[:] # Start with a copy of the array.
for i in range(len(cw)):
if cw[i] <= minWidth:
cw[i] = minWidth
if sparePixels > 0:
cw[i] += 1
sparePixels -= 1
return cw
def getNewWidths(newWidth,columnWidths):
#首先,找出我们可以均衡的列数
#不收缩任何列。
w=新宽度
n=长度(列宽度)
sortedWidths=已排序(列宽度)#数组的已排序副本。
当分拣宽度[n-1]*n>w时:
w-=分拣宽度[n-1]
n-=1
#我们可以使n个最窄的列相等。它们的新宽度是多少?
minWidth=w//n#整数除法
sparePixels=w%n#整数余数:w==minWidth*n+sparePixels
#现在生成新的列宽数组。
cw=列宽[:]#从数组的副本开始。
对于范围内的i(len(cw)):
如果cw[i]0:
cw[i]+=1
稀疏像素-=1
返回cw
Mark Ransom的答案给出了正确的算法,但如果您在了解其中的情况时遇到困难,这里有一个Python的实际实现:
def getNewWidths(newWidth, columnWidths):
# First, find out how many columns we can equalize
# without shrinking any columns.
w = newWidth
n = len(columnWidths)
sortedWidths = sorted(columnWidths) # A sorted copy of the array.
while sortedWidths[n - 1] * n > w:
w -= sortedWidths[n - 1]
n -= 1
# We can equalize the n narrowest columns. What is their new width?
minWidth = w // n # integer division
sparePixels = w % n # integer remainder: w == minWidth*n + sparePixels
# Now produce the new array of column widths.
cw = columnWidths[:] # Start with a copy of the array.
for i in range(len(cw)):
if cw[i] <= minWidth:
cw[i] = minWidth
if sparePixels > 0:
cw[i] += 1
sparePixels -= 1
return cw
def getNewWidths(newWidth,columnWidths):
#首先,找出我们可以均衡的列数
#不收缩任何列。
w=新宽度
n=长度(列宽度)
sortedWidths=已排序(列宽度)#数组的已排序副本。
当分拣宽度[n-1]*n>w时:
w-=分拣宽度[n-1]
n-=1
#我们可以使n个最窄的列相等。它们的新宽度是多少?
minWidth=w//n#整数除法
sparePixels=w%n#整数余数:w==minWidth*n+sparePixels
#现在生成新的列宽数组。
cw=列宽[:]#从数组的副本开始。
对于范围内的i(len(cw)):
如果cw[i]0:
cw[i]+=1
稀疏像素-=1
返回cw