Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TestTask
  4. {
  5.     class Program
  6.     {
  7.         /*Enter two integers n and k. Generate and print the following sequence of n elements:
  8.              The first element is: 1
  9.              All other elements = sum of the previous k elements (if less than k are available, sum all of them)
  10.              Example: n = 9, k = 5  120 = 4 + 8 + 16 + 31 + 61*/
  11.         static void Main(string[] args)
  12.         {
  13.             int n = int.Parse(Console.ReadLine());
  14.             int k = int.Parse(Console.ReadLine());
  15.             int[] arr = new int[n];
  16.             arr[0] = 1;
  17.             int currentPosition = 0;
  18.  
  19.             for(int i = 1; i < k; i++)
  20.             {
  21.                 currentPosition += arr[i-1];
  22.                 arr[i] = currentPosition;
  23.             }
  24.  
  25.             for(int i = k; i < arr.Length; i++)
  26.             {
  27.                 currentPosition = 0;
  28.                 for(int j = i-k; j < i; j++)
  29.                 {
  30.                     currentPosition += arr[j];
  31.                 }
  32.                 arr[i] = currentPosition;
  33.             }
  34.  
  35.             for(int i = 0; i < arr.Length; i++)
  36.             {
  37.                 Console.WriteLine(arr[i]);
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement