原文:

给定两个整数 nk ,任务是生成一系列 n 项,其中每个项都是之前的 k 项的和。 注:该系列的第一个术语是 1 ,如果之前的术语不够,那么其他术语应该是 0示例:

输入: n = 8, k = 3 输出: 1 1 2 4 7 13 24 44 说明: 系列生成如下: a[0]= 1 a[1]= 1 0 0 = 1 a[2]= 1 1 0 = 2 a[3]= 2 1 1 = 4 a[4]= 4 2 1 = 7 a[5]= 1 = 13 7 4 = 24 a[7]= 24 13 7 = 44 输入: n = 10,k = 4 输出:1 2 4 8 15 29 56 108 208

天真方法:想法是运行两个循环生成 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);
    }
}

python 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 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

java 描述语言


输出:

1 1 2 4 8 15 29 56 108 208 

性能分析:

  • 时间复杂度: o(n * k)
  • 空间复杂度: o(n)

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

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);
    }
}

python 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

java 描述语言


复杂度分析:

  • 时间复杂度: o(n)
  • 空间复杂度: o(n)