java - Partition 2d array in sub-arrays -
i have partition 2d array (the size given user) sub-arrays given input number user. code wrote works of instances there need with.
i taking square root of input number. example: if user inserts [10, 10, 9] means 10 * 10 array 9 sub-arrays. taking square root of 9 works fine because gives 3. if user inserts [8, 6, 6] takes square root of 6 , rounds longest side (which gives 3) , rounds down shortest (which 2). 3 * 2 = 6. works fine.
then there situation 8. square root of 8 gives 3 , 2. array partitioned 6 sub-arrays. there way find better partitioning numbers 8, 14? or there way find optimal distribution such numbers (e.g. 2 * 4 = 8, 2 * 7 = 14)?
you can calculate them bit different way:
int x = math.round(math.sqrt(n)); int y = math.round(1. * n / x);
thus you'll receive:
n = 8 => x = 3, y = 3 n = 14 => x = 4, y = 4
Comments
Post a Comment