gpt4 book ai didi

python - 根据产品数量计算 PDF 中的页面

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:39:49 26 4
gpt4 key购买 nike

我正在创建一个将 html 产品目录转换为 PDF 的网络应用程序。我正在尝试根据产品数量计算页数,将产品总数传递给函数并返回页数的整数。

由于显示横幅,不同页面上的产品数量会发生变化。

  • 第一页可以容纳 8 个产品
  • 最后一页可以容纳 12 个产品
  • 如果总共至少有 2 页,则始终存在最后一页
  • 中间页可以容纳 14 个产品
  • 除第一页和最后一页外,每组 2x 页都显示中间页
  • 如果页数为奇数,则倒数第二页将包含 16 个产品

给出一个更直观的目录页数示例:

1 page: [8]  
2 pages: [8][12]
3 pages: [8][16][12]
4 pages: [8][14][14][12]
5 pages: [8][14][14][16][12]
6 pages: [8][14][14][14][14][12]

我只是无法解决这个问题,我的数学和算法技能达不到标准。如果有人能阐明我应该前进的方向,我将不胜感激。

最佳答案

从逻辑上讲,您应该先放置第一页,然后是最后一页,然后检查总数是否为奇数,然后添加中间页。

def ass(num):
#Check for negative number
if num <= 0: return
#This list will be returned, start with 8, since it always has 8
ret = [8]

#If only on number, then return
if num == 1: return ret
#add 14s in the middle; even need total-2, odd number needs total-3
to_add = num - 2 if num % 2 == 0 else num - 3

for i in range(to_add):
ret.append(14)

#check for second to last if odd, we need 16 appended
if num % 2 != 0:
ret.append(16)
#Final 12
ret.append(12)

return ret

关于python - 根据产品数量计算 PDF 中的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45094162/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com