Finding the optimum column and row size for a table with n elements and a given range for its proportion

后端 未结 2 1556
无人共我
无人共我 2021-01-22 09:36

I am looking for an optimum way to create a table from n elements so that ideally there are no empty cells, but at the same time the proportion of the table dimensions columns /

相关标签:
2条回答
  • 2021-01-22 09:52

    Here is some pseudocode for how I've implemented something similarly:

    int rowCount;
    int colCount;
    
    double tempSQR = SquareRoot(cellCount);
    int maxRowCount = RoundAwayFromZero(tempSQR);
    
    if(tempSQR == maxRowCount)
    {
       rowCount = maxRowCount;
       colCount = rowCount;
    }
    else if(cellCount is Even)
    {
       rowCount = Min(cellCount/2, maxRowCount);
       colCount = RoundAwayFromZero(cellCount/rowCount);
    }
    else if(cellCount> 1)
    {
       rowCount = Min((cellCount+ 1)/2, maxRowCount);
       colCount = RoundAwayFromZero((cellCount+ 1)/rowCount);
    }
    
    if(rowCount * colCount < cellCount)
        rowCount++;
    
    0 讨论(0)
  • 2021-01-22 09:54

    Instead of doing a prime factorization of n, start from the square root and find the next larger (or smaller -- makes no difference) factor. That pair of factors will the closest to the square root, and therefore the closest to a proportion of 1:1.

    0 讨论(0)
提交回复
热议问题