C++ 打印给定第n个项的总和的程序

C++ 打印给定第n个项的总和的程序

问题说明包括打印序列的总和,其中给出了第N个项。

输入中将给出N的值。我们需要找到序列的总和,其中序列的第N个项由以下公式给出:

N^{2}−(N−1)^{2}

让我们通过以下示例来理解这个问题:

输入

N=5

输出

25

解释 - 给定的N的值为5。序列的前5个项为:

\mathrm{N=1,1^{2}−(1−1)^{2}=1}

\mathrm{N=2,2^{2}−(2−1)^{2}=3}

\mathrm{N=3,3^{2}−(3−1)^{2}=5}

\mathrm{N=4,4^{2}−(4−1)^{2}=7}

\mathrm{N=5,5^{2}−(5−1)^{2}=9}

序列的前5个项的和为1+3+5+7+9=25。因此,输出为25。

输入

N=8

输出

64

说明 − 使用序列第 N 项的公式计算前 8 项的和,

\mathrm{N=1,1^{2}−(1−1)^{2}=1}

\mathrm{N=2,2^{2}−(2−1)^{2}=3}

\mathrm{N=3,3^{2}−(3−1)^{2}=5}

\mathrm{N=4,4^{2}−(4−1)^{2}=7}

\mathrm{N=5,5^{2}−(5−1)^{2}=9}

\mathrm{N=6,6^{2}−(6−1)^{2}=11}

\mathrm{N=7,7^{2}−(7−1)^{2}=13}

\mathrm{N=8,8^{2}−(8−1)^{2}=15}
和将为 1+3+5+7+9+11+13+15=64,这是所需的输出。

让我们看看在 C++ 中解决这个问题的不同方法。

方法 1

问题的朴素解决方法可能是计算直到 N 的序列中的每一项,并将它们相加以获得序列的和,其中序列的第 N 项是 \mathrm{N^{2}−(N−1)^{2}}

实现方法来获得和直到 N 的序列的步骤:

  • 我们将简单地创建一个函数来计算序列直到它的第 N 项的和,其中序列的每一项由 \mathrm{N^{2}−(N−1)^{2}} 给出。

  • 初始化一个变量来存储 N 项序列的和。该变量必须是 long long 数据类型,以确保对于较大的 N 值的 sum 的值。

  • 在 for 循环中迭代 i=0 到 i<=N,以计算 N 项序列的和。

  • 对于每次迭代,我们将使用公式 \mathrm{N^{2}−(N−1)^{2}} 将第 i 项的和添加到变量中,因为它表示序列的第 N 项。

  • 在迭代 N 次后,返回变量,它将是前 N 项序列的和,其中 N 是用户输入。

示例

//C++ code to find the sum of the sequence of first N terms where
//Nth term is given by N^2-(N-1)^2

#include <bits/stdc++.h>

using namespace std;

//function to calculate the sum of the sequence until N
long long sum(int N){

    long long a=0; //to store the sum of the sequence

    //iterating to calculate every term of the sequence from 1 to N
    for(int i=1;i<=N;i++){

        //using the formula N^2-(N-1)^2
        a = a + (i * i - (i-1)*(i-1)); //adding each term of the sequence until N
    }

    return a;
}

int main()
{
    int N;
    N=8; //input
    //calling the function
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;

    N=54;
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;

    return 0;
}

输出

The sum of the sequence up to 8 terms is: 64
The sum of the sequence up to 54 terms is: 2916

时间复杂度 − O(N),因为我们使用for循环迭代N次来计算序列的每一项并将它们相加。

空间复杂度 − O(1),因为没有使用额外的空间来计算总和。

方法−2(高效方法)

尽管可以使用序列第N项的公式同时计算序列的每个项并将它们相加,但由于该序列形成了等差数列(A.P.)的和的概念,我们可以使用求前N项等差数列和的方式来计算。

如果我们使用序列第N项的公式\mathrm{N^{2}-(N−1)^{2}}计算序列的前几个数字并观察模式,我们得到

序列的第一个数字是1。

序列的第二个数字是3。

同样,
\mathrm{N=3,3^{2}−(3−1)^{2}=5}

\mathrm{N=4,4^{2}−(4−1)^{2}=7}

\mathrm{N=5,5^{2}−(5−1)^{2}=9}

使用公式计算的序列前几个数字是1, 3, 5, 7, 9, 11, 13……。

我们可以看出,该序列只是以1为首项,2为公差的等差数列。

等差数列前N项的和由以下公式给出,

\mathrm{S_{N}=\frac{N}{2}(2*a+(N−1)d)},其中a为等差数列的首项,d为等差数列的公差。

要计算序列的前N项之和,我们将使用以上公式。代入数值,我们得到

\mathrm{序列的和=\frac{N}{2}(2*1+(N−1)2):(其中a=1:和:d=2)}

\mathrm{=\frac{2*N}{2}(1+N−1)=N^{2}}

序列的和,直到第N项,其中N项由\mathrm{N^{2}−(N−1)^{2}}给出,等于N的平方,其中N是项数。

在C++中实现该方法时,遵循以下步骤:

  • 我们将创建一个函数来获取序列的和,直到第N项,其中N项为\mathrm{N^{2}−(N−1)^{2}}

  • 初始化一个变量来存储序列的和,并将N的平方存储在其中,因为它给出了前N项序列的和。

  • 返回该变量,这将是所需的输出。

示例

//C++ code to find the sum of the sequence of first N terms where
//Nth term is given by N^2-(N-1)^2

#include <bits/stdc++.h>

using namespace std;

//function to calculate the sum of the sequence until N terms
long long sum(int N){

    long long a=0; //to store the sum of the sequence

    a = N*N; //the formula to give the sum of sequence up to N terms

    return a;
}

int main()
{
    int N;
    N=75; //input

    //calling the function
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;

    N=1000;
    cout<<"The sum of the sequence up to "<<N<<" terms is: "<<sum(N)<<endl;

    return 0;
}

输出

The sum of the sequence up to 75 terms is: 5625
The sum of the sequence up to 1000 terms is: 1000000

时间复杂度 − O(1),计算序列的和只需常数时间。

空间复杂度 − O(1),不需要额外空间。

结论

本文讨论了一个问题:找到第N个项为 \mathrm{N^{2}−(N−1)^{2}} 的序列的和。我们讨论了解决这个问题的朴素方法,以及在C++中用常数时间和空间找到序列前N项和的高效解决方案。

希望您阅读本文后能够理解问题和解决问题的方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程