日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

notepad++ 偶数行_C ++程序查找前N个偶数的立方和

發布時間:2025/3/11 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 notepad++ 偶数行_C ++程序查找前N个偶数的立方和 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

notepad++ 偶數行

The problem is we have a number N and we have to find sum of first N Even natural numbers.

問題是我們有一個數N ,我們必須找到前N個偶數自然數之和。

Example:

例:

Input:n = 3Output:288 (2^3 + 4^3+6^3)

A simple solution is given below...

下面給出一個簡單的解決方案 ...

Example 1:

范例1:

#include <iostream> using namespace std; int calculate(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + (2*i) * (2*i) * (2*i); return sum; } int main() {int num = 3;cout<<"Number is = "<<num<<endl; cout << "Sum of cubes of first "<<num<<" even number is ="<<calculate(num); return 0; }

Output

輸出量

Number is = 3 Sum of cubes of first 3 even number is =288

The efficient approach is discussed below:

下面討論了有效的方法 :

The sum of cubes of first n natural numbers is given by = (n*(n+1) / 2)^2 Sum of cubes of first n natural numbers can be written as... = 2^3 + 4^3 + .... + (2n)^3 Now take out common term i.e 2^3 = 2^3 * (1^3 + 2^3 + .... + n^3)= 2^3* (n*(n+1) / 2)^2= 8 * ((n^2)(n+1)^2)/4= 2 * n^2(n+1)^2

Now we can apply this formula directly to find the sum of cubes of first n even numbers.

現在我們可以直接應用此公式來找到前n個偶數的立方和 。

Example 2:

范例2:

#include <iostream> using namespace std; int calculate(int n) {int sum = 2 * n * n * (n + 1) * (n + 1);return sum; } int main() {int num = 3;cout<<"Number is = "<<num<<endl; cout << "Sum of cubes of first "<<num<<" even number is ="<<calculate(num); return 0; }

Output

輸出量

Number is = 3 Sum of cubes of first 3 even number is =288

翻譯自: https://www.includehelp.com/cpp-programs/find-sum-of-cubes-of-first-n-even-numbers.aspx

notepad++ 偶數行

總結

以上是生活随笔為你收集整理的notepad++ 偶数行_C ++程序查找前N个偶数的立方和的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。