Advertisement
VasilKotsev

Fibonacci sequence with stack and queue

Jan 19th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. namespace SandBox
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class EntryPoint
  7.     {
  8.         public static void Main()
  9.         {
  10.             Queue<long> fiboSeq = new Queue<long>();
  11.  
  12.             long inputNumber = long.Parse(Console.ReadLine());
  13.  
  14.             fiboSeq.Enqueue(0);
  15.             fiboSeq.Enqueue(1);
  16.  
  17.             if (inputNumber == 0)
  18.             {
  19.                 Console.WriteLine(0);
  20.                 return;
  21.             }
  22.  
  23.             for (int i = 0; i < inputNumber - 1; i++)
  24.             {
  25.                 Stack<long> tempStack = new Stack<long>(fiboSeq);
  26.                 long currentNumber = tempStack.Pop();
  27.                 long previousNumber = tempStack.Pop();
  28.  
  29.                 fiboSeq.Enqueue(currentNumber + previousNumber);
  30.             }
  31.  
  32.             // Removes the 0 because the problem description states that
  33.             // the fibojacci sequence should start from 1.
  34.             fiboSeq.Dequeue();
  35.  
  36.             string output = string.Join(" ", fiboSeq);
  37.             Console.WriteLine(output);
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement