Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT: Fixed issue with even hours and items.</strong></p> <p>This is a hard problem and below is the solution. I've written solution for a completely generic problem so it works for arbitrary hours and number of items. Here's the example outputs:</p> <pre><code>Items=10, Hours=14 XX XX XX XX XX Items=11, Hours=14 XX XXXXX XX XX Items=12, Hours=14 XX XXXXXXXX XX Items=16, Hours=13 XXXXXXXXXXXXX XXX Items=17, Hours=13 XXXXXXXXXXXXX X X X X Items=18, Hours=13 XXXXXXXXXXXXX X XXX X Items=19, Hours=13 XXXXXXXXXXXXX X X X X X X Items=20, Hours=13 XXXXXXXXXXXXX X X XXX X X Items=21, Hours=13 XXXXXXXXXXXXX X XX X X XX X </code></pre> <p>Here's how below solution works:</p> <ol> <li>Number of filled lines are trivial which you can get it by (items/hours) * hours.</li> <li>The last line is where all the magic is required.</li> <li>When number of remaining items are odd we want to turn on the center. If number of hours are also odd then center is well defined but otherwise we are out of luck and we would have some "imbalance" in that case.</li> <li>For even items we make them in to pairs and distribute each pair in the order of balanced binary tree. This basically means we first put each pair at the end. Then next pair half way through and recursively follow the pattern. This might be the most difficult part to understand so paper and pen is recommended :).</li> </ol> <p>And here's the code:</p> <pre><code>static void Main(string[] args) { var hours = 13; for (var items = 16; items &lt; 22; items++) PrintDistribution(items, hours); } private static void PrintDistribution(int items, int hours) { Console.WriteLine(string.Format("\nItems={0}, Hours={1}", items, hours)); for (var i = 0; i &lt; (items / hours) * hours; i++) { Console.Write('X'); if ((i + 1) % hours == 0) Console.WriteLine(); } var line = new StringBuilder(new string(' ', hours)); var remaining = items % hours; var evens = remaining / 2; var odd = remaining - (evens * 2); var seq = BinaryTreeSequence(hours / 2).GetEnumerator(); for (var i = 0; i &lt; evens; i++) { seq.MoveNext(); line[seq.Current] = 'X'; line[hours - seq.Current - 1] = 'X'; } if (odd &gt; 0) if (hours % 2 == 0) { seq.MoveNext(); line[seq.Current] = 'X'; } else line[hours / 2] = 'X'; Console.WriteLine(line); } public static IEnumerable&lt;int&gt; BinaryTreeSequence(int count) { if (count &gt; 1) yield return count - 1; if (count &gt; 0) yield return 0; var seqQueue = new Queue&lt;Tuple&lt;int, int, int&gt;&gt;(); Enqueue(seqQueue, 0, count - 1); for (var seqIndex = count - 2; seqIndex &gt; 0; seqIndex--) { var moreNeeded = seqQueue.Count &lt; seqIndex; var seq = seqQueue.Dequeue(); yield return seq.Item1; if (moreNeeded) { Enqueue(seqQueue, seq.Item1, seq.Item3); Enqueue(seqQueue, seq.Item2, seq.Item1); } } } private static void Enqueue(Queue&lt;Tuple&lt;int, int, int&gt;&gt; q, int min, int max) { var midPoint = (min + max) / 2; if (midPoint != min &amp;&amp; midPoint != max) q.Enqueue(Tuple.Create(midPoint, min, max)); } </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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