Advertisement
Guest User

SequenceNM

a guest
Jul 18th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace _09.Sequence_NM
  5. {
  6.     enum Operation
  7.     {
  8.         PlusOne,
  9.         PlusTwo,
  10.         TimesTwo
  11.     }
  12.  
  13.     static class SequenceNM
  14.     {
  15.         static void Main(string[] args)
  16.         {
  17.             var input = Console.ReadLine().Split(' ');
  18.             int start = Int32.Parse(input[0]);
  19.             int end = Int32.Parse(input[1]);
  20.  
  21.         if (end < start) return;
  22.  
  23.             Stack<Operation> operationStack = new Stack<Operation>();
  24.  
  25.             while (end != start)
  26.             {
  27.                 if (end > start * 2) // If we can divide by 2
  28.                 {
  29.                     if (end % 2 == 1) // Must be even to devide, so if it's not - subtract 1
  30.                     {
  31.                         operationStack.Push(Operation.PlusOne);
  32.                         end--;
  33.                     }
  34.  
  35.                     operationStack.Push(Operation.TimesTwo);
  36.                     end /= 2;
  37.                 }
  38.  
  39.                 else // If we CANNOT divide by 2 (end < start * 2)
  40.                 {
  41.                     if (start % 2 != end % 2) // If they're not both even or both odd, make them by subracting 1
  42.                     {
  43.                         operationStack.Push(Operation.PlusOne);
  44.                         end--;
  45.                         continue; // e.g. start = 2, end = 3; end -> 2 so we don't need to subtract 2, they are equal, exit the loop
  46.                     }
  47.  
  48.                     operationStack.Push(Operation.PlusTwo); // When they're both odd/even just keep subtracting 2 until they are equal
  49.                     end -= 2;
  50.                 }
  51.             }
  52.  
  53.             /*
  54.              * Note! We can speed up this algorithm by printing the output directly in the loop above while calculating, but we would have to operate from start to end, or to insert at the beginning of a string:
  55.              * 10 - 30
  56.              * 30
  57.              * 15 -> 30
  58.              * 14 -> 15 -> 30
  59.              * 12 -> 14 -> 15 -> 30
  60.              * 10 -> 12 -> 14 -> 15 -> 30
  61.              */
  62.  
  63.             Console.Write(start);
  64.             while (operationStack.Count > 0)
  65.             {
  66.                 Console.Write(" -> ");
  67.                 switch (operationStack.Pop()) // Perform the last operation on the start variable and print it
  68.                 {
  69.                     case Operation.PlusOne:
  70.                         start += 1;
  71.                         break;
  72.                     case Operation.PlusTwo:
  73.                         start += 2;
  74.                         break;
  75.                     case Operation.TimesTwo:
  76.                         start *= 2;
  77.                         break;
  78.                 }
  79.                 Console.Write(start);
  80.             }
  81.             Console.WriteLine();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement