BorislavBorisov

Редици Find index in sequence N, N+1, 2*N

Oct 3rd, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class FindIndex
  5. {
  6.     static void Main()
  7.     {
  8.         int n = 3;
  9.         int p = 16;
  10.          
  11.         Queue<int> queue = new Queue<int>();
  12.         queue.Enqueue(n);
  13.         int index = 0;
  14.         while (queue.Count > 0)
  15.         {
  16.             int current = queue.Dequeue();
  17.             index++;
  18.             if (current == p)
  19.             {
  20.                 Console.WriteLine("index = {0}", index);
  21.                 break;
  22.             }
  23.             queue.Enqueue(current + 1);
  24.             queue.Enqueue(2 * current);
  25.         }
  26.     }    
  27. }
Advertisement
Add Comment
Please, Sign In to add comment