📌  相关文章
📜  打印大小为 N 的序列,其中每一项都是前 K 项的总和

📅  最后修改于: 2021-09-06 11:26:40             🧑  作者: Mango

给定两个整数NK ,任务是生成一系列N项,其中每一项都是前K项的总和。
注意:系列的第一项是1 ,如果前项不够,那么其他项应该是0
例子:

朴素的方法:这个想法是运行两个循环来生成 N 项系列。下面是步骤的图示:

  • 0N – 1遍历第一个循环,以生成系列的每一项。
  • 运行从max(0, i – K)i的循环以计算前 K 项的总和。
  • 将总和更新为系列的当前索引作为当前项。

下面是上述方法的实现:

C++
// C++ implementation to find the
// series in which every term is
// sum of previous K terms
 
#include 
 
using namespace std;
 
// Function to generate the
// series in the form of array
void sumOfPrevK(int N, int K)
{
    int arr[N];
    arr[0] = 1;
 
    // Pick a starting point
    for (int i = 1; i < N; i++) {
        int j = i - 1, count = 0,
            sum = 0;
        // Find the sum of all
        // elements till count < K
        while (j >= 0 && count < K) {
            sum += arr[j];
            j--;
            count++;
        }
        // Find the value of
        // sum at i position
        arr[i] = sum;
    }
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 10, K = 4;
    sumOfPrevK(N, K);
    return 0;
}


Java
// Java implementation to find the
// series in which every term is
// sum of previous K terms
 
class Sum {
    // Function to generate the
    // series in the form of array
    void sumOfPrevK(int N, int K)
    {
        int arr[] = new int[N];
        arr[0] = 1;
 
        // Pick a starting point
        for (int i = 1; i < N; i++) {
            int j = i - 1, count = 0,
                sum = 0;
            // Find the sum of all
            // elements till count < K
            while (j >= 0 && count < K) {
                sum += arr[j];
                j--;
                count++;
            }
            // Find the value of
            // sum at i position
            arr[i] = sum;
        }
        for (int i = 0; i < N; i++) {
            System.out.print(arr[i] + " ");
        }
    }
 
    // Driver Code
    public static void main(String args[])
    {
        Sum s = new Sum();
        int N = 10, K = 4;
        s.sumOfPrevK(N, K);
    }
}


Python3
# Python3 implementation to find the
# series in which every term is
# sum of previous K terms
 
# Function to generate the
# series in the form of array
def sumOfPrevK(N, K):
    arr = [0 for i in range(N)]
    arr[0] = 1
 
    # Pick a starting point
    for i in range(1,N):
        j = i - 1
        count = 0
        sum = 0
         
        # Find the sum of all
        # elements till count < K
        while (j >= 0 and count < K):
            sum = sum + arr[j]
            j = j - 1
            count = count + 1
 
        # Find the value of
        # sum at i position
        arr[i] = sum
 
    for i in range(0, N):
        print(arr[i])
 
# Driver Code
N = 10
K = 4
sumOfPrevK(N, K)
 
# This code is contributed by Sanjit_Prasad


C#
// C# implementation to find the
// series in which every term is
// sum of previous K terms
using System;
 
class Sum {
    // Function to generate the
    // series in the form of array
    void sumOfPrevK(int N, int K)
    {
        int []arr = new int[N];
        arr[0] = 1;
  
        // Pick a starting point
        for (int i = 1; i < N; i++) {
            int j = i - 1, count = 0,
                sum = 0;
 
            // Find the sum of all
            // elements till count < K
            while (j >= 0 && count < K) {
                sum += arr[j];
                j--;
                count++;
            }
 
            // Find the value of
            // sum at i position
            arr[i] = sum;
        }
        for (int i = 0; i < N; i++) {
            Console.Write(arr[i] + " ");
        }
    }
  
    // Driver Code
    public static void Main(String []args)
    {
        Sum s = new Sum();
        int N = 10, K = 4;
        s.sumOfPrevK(N, K);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ implementation to find the
// series in which every term is
// sum of previous K terms
 
#include 
 
using namespace std;
 
// Function to generate the
// series in the form of array
void sumOfPrevK(int N, int K)
{
    int arr[N], prevsum = 0;
    arr[0] = 1;
 
    // Pick a starting point
    for (int i = 0; i < N - 1; i++) {
 
        // Computing the previous sum
        if (i < K) {
            arr[i + 1] = arr[i] + prevsum;
            prevsum = arr[i + 1];
        }
        else {
            arr[i + 1] = arr[i] + prevsum
                         - arr[i + 1 - K];
            prevsum = arr[i + 1];
        }
    }
 
    // Loop to print the series
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 8, K = 3;
    sumOfPrevK(N, K);
    return 0;
}


Java
// Java implementation to find the
// series in which every term is
// sum of previous K terms
 
class Sum {
    // Function to generate the
    // series in the form of array
    void sumOfPrevK(int N, int K)
    {
        int arr[] = new int[N];
        int prevsum = 0;
        arr[0] = 1;
 
        // Pick a starting point
        for (int i = 0; i < N - 1; i++) {
 
            // Computing the previous sum
            if (i < K) {
                arr[i + 1] = arr[i] + prevsum;
                prevsum = arr[i + 1];
            }
            else {
                arr[i + 1] = arr[i] + prevsum
                             - arr[i + 1 - K];
                prevsum = arr[i + 1];
            }
        }
 
        // Loop to print the series
        for (int i = 0; i < N; i++) {
            System.out.print(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        Sum s = new Sum();
        int N = 8, K = 3;
        s.sumOfPrevK(N, K);
    }
}


Python3
# Python3 implementation to find the
# series in which every term is
# sum of previous K terms
 
# Function to generate the
# series in the form of array
def sumOfPrevK(N, K):
    arr = [0]*N;
    prevsum = 0;
    arr[0] = 1;
 
    # Pick a starting point
    for i in range(N-1):
 
        # Computing the previous sum
        if (i < K):
            arr[i + 1] = arr[i] + prevsum;
            prevsum = arr[i + 1];
        else:
            arr[i + 1] = arr[i] + prevsum - arr[i + 1 - K];
            prevsum = arr[i + 1];
 
    # Loop to print the series
    for i in range(N):
        print(arr[i], end=" ");
     
# Driver code
if __name__ == '__main__':
    N = 8;
    K = 3;
    sumOfPrevK(N, K);
         
# This code is contributed by 29AjayKumar


C#
// C# implementation to find the
// series in which every term is
// sum of previous K terms
using System;
public class Sum {
    // Function to generate the
    // series in the form of array
    void sumOfPrevK(int N, int K)
    {
        int []arr = new int[N];
        int prevsum = 0;
        arr[0] = 1;
  
        // Pick a starting point
        for (int i = 0; i < N - 1; i++) {
  
            // Computing the previous sum
            if (i < K) {
                arr[i + 1] = arr[i] + prevsum;
                prevsum = arr[i + 1];
            }
            else {
                arr[i + 1] = arr[i] + prevsum
                             - arr[i + 1 - K];
                prevsum = arr[i + 1];
            }
        }
  
        // Loop to print the series
        for (int i = 0; i < N; i++) {
            Console.Write(arr[i] + " ");
        }
    }
  
    // Driver code
    public static void Main(String []args)
    {
        Sum s = new Sum();
        int N = 8, K = 3;
        s.sumOfPrevK(N, K);
    }
}
 
// This code is contributed by 29AjayKumar


输出:

1 1 2 4 8 15 29 56 108 208 

性能分析:

  • 时间复杂度: O(N * K)
  • 空间复杂度: O(N)

有效的方法:想法是将当前和存储在一个变量中,并在每一步中减去最后的K项并将最后一项添加到预和中以计算序列的每一项。
下面是上述方法的实现:

C++

// C++ implementation to find the
// series in which every term is
// sum of previous K terms
 
#include 
 
using namespace std;
 
// Function to generate the
// series in the form of array
void sumOfPrevK(int N, int K)
{
    int arr[N], prevsum = 0;
    arr[0] = 1;
 
    // Pick a starting point
    for (int i = 0; i < N - 1; i++) {
 
        // Computing the previous sum
        if (i < K) {
            arr[i + 1] = arr[i] + prevsum;
            prevsum = arr[i + 1];
        }
        else {
            arr[i + 1] = arr[i] + prevsum
                         - arr[i + 1 - K];
            prevsum = arr[i + 1];
        }
    }
 
    // Loop to print the series
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
}
 
// Driver Code
int main()
{
    int N = 8, K = 3;
    sumOfPrevK(N, K);
    return 0;
}

Java

// Java implementation to find the
// series in which every term is
// sum of previous K terms
 
class Sum {
    // Function to generate the
    // series in the form of array
    void sumOfPrevK(int N, int K)
    {
        int arr[] = new int[N];
        int prevsum = 0;
        arr[0] = 1;
 
        // Pick a starting point
        for (int i = 0; i < N - 1; i++) {
 
            // Computing the previous sum
            if (i < K) {
                arr[i + 1] = arr[i] + prevsum;
                prevsum = arr[i + 1];
            }
            else {
                arr[i + 1] = arr[i] + prevsum
                             - arr[i + 1 - K];
                prevsum = arr[i + 1];
            }
        }
 
        // Loop to print the series
        for (int i = 0; i < N; i++) {
            System.out.print(arr[i] + " ");
        }
    }
 
    // Driver code
    public static void main(String args[])
    {
        Sum s = new Sum();
        int N = 8, K = 3;
        s.sumOfPrevK(N, K);
    }
}

蟒蛇3

# Python3 implementation to find the
# series in which every term is
# sum of previous K terms
 
# Function to generate the
# series in the form of array
def sumOfPrevK(N, K):
    arr = [0]*N;
    prevsum = 0;
    arr[0] = 1;
 
    # Pick a starting point
    for i in range(N-1):
 
        # Computing the previous sum
        if (i < K):
            arr[i + 1] = arr[i] + prevsum;
            prevsum = arr[i + 1];
        else:
            arr[i + 1] = arr[i] + prevsum - arr[i + 1 - K];
            prevsum = arr[i + 1];
 
    # Loop to print the series
    for i in range(N):
        print(arr[i], end=" ");
     
# Driver code
if __name__ == '__main__':
    N = 8;
    K = 3;
    sumOfPrevK(N, K);
         
# This code is contributed by 29AjayKumar

C#

// C# implementation to find the
// series in which every term is
// sum of previous K terms
using System;
public class Sum {
    // Function to generate the
    // series in the form of array
    void sumOfPrevK(int N, int K)
    {
        int []arr = new int[N];
        int prevsum = 0;
        arr[0] = 1;
  
        // Pick a starting point
        for (int i = 0; i < N - 1; i++) {
  
            // Computing the previous sum
            if (i < K) {
                arr[i + 1] = arr[i] + prevsum;
                prevsum = arr[i + 1];
            }
            else {
                arr[i + 1] = arr[i] + prevsum
                             - arr[i + 1 - K];
                prevsum = arr[i + 1];
            }
        }
  
        // Loop to print the series
        for (int i = 0; i < N; i++) {
            Console.Write(arr[i] + " ");
        }
    }
  
    // Driver code
    public static void Main(String []args)
    {
        Sum s = new Sum();
        int N = 8, K = 3;
        s.sumOfPrevK(N, K);
    }
}
 
// This code is contributed by 29AjayKumar

复杂度分析:

  • 时间复杂度: O(N)
  • 空间复杂度: O(N)