题目见与hihocoder,大意就是在已知书的尺寸往里面塞字符,要求总的页码小于一个值。字符以自然段的形式存在。
import math n = int(raw_input()) # N P W N for i in range(n): x1 = raw_input().strip("\n").split(" ") x1 = map(int,x1) x2 = raw_input().strip("\n").split(" ") x2 = map(int,x2) n,p,w,h = x1[0],x1[1],x1[2],x1[3] min_ = min(w,h) for size in range(min_,0,-1): rows = 0 per_row = w // size for d in x2: rows += math.ceil((d+0.0) / per_row) page_row = h // size pages = math.ceil((rows+0.0)/ page_row) if pages <= p: print size break
总结:
1、//运算符,表示无条件的向下取整,即使是浮点数也如此。
如
x1 =3.0 x2 = 3 y =2 z1 = x1 / y #1.5 z2 = x1 // y #1.0 z3 = x2 / y #1 z4 = x2 //y #12、关于读取数据的方法,对于已知循环次数的时候,应该在开始时用上for循环,而不是企图一股脑的把数据读进去,再用列表各种装。