Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand correctly the requirement is:</p> <pre><code>If the number is 3n, divide it in 3 groups of n, n and n elements. If the number is 3n+1, divide it in 3 groups of n+1, n and n elements. If the number is 3n+2, divide it in 3 groups of n+1, n+1 and n elements. </code></pre> <p>Best thing to do is to make that explicit in your code, and avoid any "clever" logic. The straight-forward splitting boils down to:</p> <pre><code>If the number is 3n, the divisions are: 0 .. n-1 n .. 2n-1 2n .. 3n-1 If the number is 3n+1, the divisions are: 0 .. n n+1 .. 2n 2n+1 .. 3n If the number is 3n+2, the divisions are: 0 .. n n+1 .. 2n+1 2n+2 .. 3n+1 </code></pre> <p>In c# that would be something like:</p> <pre><code>public static List&lt;int&gt; Divide3Columns(int total, int column) { int startIndex = 0; int endIndex = 0; int pageSize = total / 3; if (total % 3 == 0) { startIndex = column * pageSize; endIndex = (column + 1) * pageSize - 1; } if (total % 3 == 1) { if (column == 0) { startIndex = 0; endIndex = pageSize; //pageSize + 1 elements; } else { startIndex = column * pageSize + 1; endIndex = (column + 1) * pageSize; } } if (total % 3 == 2) { if (column == 2) { startIndex = 2 * pageSize + 2; endIndex = 3 * pageSize + 1; //same as total - 1; } else { startIndex = column * (pageSize + 1); endIndex = (column + 1) * pageSize + column; } } List&lt;int&gt; result = new List&lt;int&gt;(); for (int i = startIndex; i &lt;= endIndex; i++) { result.Add(i); } return result; } </code></pre> <hr> <p>The answer assumes that we will always <a href="http://en.wikipedia.org/wiki/Holy_Hand_Grenade" rel="nofollow">divide in 3, and only 3 columns</a>. If the number is variable, the logic to determine the columns can be generalized.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload