another90sm

Stack_Fibonacci

May 24th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class StackFibonacci
  7. {
  8.     static void Main()
  9.     {
  10.         Stack<long> fibonacii = new Stack<long>();
  11.         int n = int.Parse(Console.ReadLine());
  12.         fibonacii.Push(1);
  13.         fibonacii.Push(1);
  14.  
  15.         for (int i = 0; i < n - 2; i++)
  16.         {
  17.             long a = fibonacii.Pop();
  18.             long b = fibonacii.Peek();
  19.             fibonacii.Push(a);
  20.             fibonacii.Push(a + b);
  21.         }
  22.         Console.WriteLine(fibonacii.Peek());
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment