简体   繁体   English

x4网格中的行数公式

[英]Formula for number of rows from a x4 grid

Ok so I am currently setting up a grid, each row will have 4 objects. 好的,所以我当前正在设置一个网格,每行将有4个对象。 The number of rows must be calculated according to this constraint. 行数必须根据此约束进行计算。

So if I had 15 objects, I need 4 rows. 因此,如果我有15个对象,则需要4行。 If i had 12 objects I need 3 rows. 如果我有12个对象,我需要3行。

Somehow I need some sort of mathematical formula that will perform and return this number. 我不知何故需要某种数学公式来执行并返回该数字。 So when I say to the function I have x number of objects, it will return the number of rows. 因此,当我对函数说我有x个对象时,它将返回行数。

Thanks for any help. 谢谢你的帮助。

ceil(x / 4.0)

You want the ceiling, not the floor of the resultant value. 您需要上限,而不是结果值的下限。 Otherwise, you won't be able to fit say 15 into 4 rows. 否则,您将无法将15放入4行。

Warning: readability swamp 警告:可读性沼泽

floor((obj - 1) / 4) + 1 楼板((obj-1)/ 4)+ 1

where obj is the number of objects in your question. 其中obj是您问题中的对象数。

or even more concise: 甚至更简洁:

floor((obj + 3) / 4) 地板((obj + 3)/ 4)

One liner! 一班!

Floor usually comes with the language's integer division operator (eg, Java, C++), so it's probably shorter to implement. Floor通常带有语言的整数除法运算符(例如Java,C ++),因此实现起来可能要短一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM