- import static org.junit.Assert.assertEquals;
- import org.junit.Test;
- public class TestOthers {
- @Test
- public void test() {
- // size columns expect
- int MAX_COLUMNS = 3;
- int[][] sources = new int[][] { { 1, MAX_COLUMNS, 1 },
- { 2, MAX_COLUMNS, 1 }, { 3, MAX_COLUMNS, 1 },
- { 4, MAX_COLUMNS, 2 }, { 5, MAX_COLUMNS, 2 },
- { 6, MAX_COLUMNS, 2 }, { 7, MAX_COLUMNS, 3 }, };
- for (int[] arr : sources) {
- assertEquals(arr[2], rowsOf(arr[0], arr[1]));
- }
- }
- /**
- * 已知总数量和列数求行数
- *
- * @param size
- * 总数量
- * @param columns
- * 列数
- * @return 计算得到的行数
- */
- public static int rowsOf(int size, int columns) {
- if (size < 1 || columns < 1)
- return 0;
- // 整除
- boolean isDivisible = (size % columns) == 0;
- if (isDivisible) {
- return size / columns;
- } else {
- return size / columns + 1;
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/280220148844.html
来源: http://www.codesnippet.cn/detail/280220148844.html