tanya_zheleva

Last K Numbers

Jan 30th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace ExamPreparation
  6. {
  7.     class Startup
  8.     {
  9.         static void Main()
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             int k = int.Parse(Console.ReadLine());
  13.             long[] numbers = new long[n];
  14.             List<long> summed = new List<long>();
  15.  
  16.  
  17.             numbers[0] = 1;
  18.             summed.Add(numbers[0]);
  19.             int counter = 1;
  20.  
  21.             for (int i = 1; i < numbers.Length; i++)
  22.             {
  23.                 counter++;
  24.                 if (counter > k)
  25.                 {
  26.                     counter = 1;
  27.                 }
  28.  
  29.                 numbers[i] = summed.Sum();
  30.                 summed.Add(numbers[i]);
  31.  
  32.                 if (summed.Count > k)
  33.                 {
  34.                     summed.RemoveAt(0);
  35.                 }
  36.             }
  37.            
  38.             Console.WriteLine(string.Join(" ", numbers));
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment