SHARE
TWEET

Untitled




Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- using System;
- namespace TestTask
- {
- class Program
- {
- /*Enter two integers n and k. Generate and print the following sequence of n elements:
- The first element is: 1
- All other elements = sum of the previous k elements (if less than k are available, sum all of them)
- Example: n = 9, k = 5 120 = 4 + 8 + 16 + 31 + 61*/
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int k = int.Parse(Console.ReadLine());
- int[] arr = new int[n];
- arr[0] = 1;
- int currentPosition = 0;
- for(int i = 1; i < k; i++)
- {
- currentPosition += arr[i-1];
- arr[i] = currentPosition;
- }
- for(int i = k; i < arr.Length; i++)
- {
- currentPosition = 0;
- for(int j = i-k; j < i; j++)
- {
- currentPosition += arr[j];
- }
- arr[i] = currentPosition;
- }
- for(int i = 0; i < arr.Length; i++)
- {
- Console.WriteLine(arr[i]);
- }
- }
- }
- }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.