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 = 3
Output:
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++ 偶数行

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545743.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Auto activation triggers for Java(代码提示)功能扩展

1.打开Eclipse 后“window”→“Preferences”&#xff0c;选择“java”&#xff0c;展开&#xff0c;“Editor”&#xff0c;选择“Content Assist”。2.选择“Content Assist”&#xff0c;然后看到右边&#xff0c;右边的“Auto-Activation”下面的“Auto Activation trigge…

Linux——回射服务器多并发(多线程)

多线程与多进程的做法区别不大&#xff0c;思路一样&#xff0c;都是执行两个死循环&#xff0c;一个循环等待客户端连接&#xff0c;一个循环与客户端通信。多进程的方式请点此处 服务器 #include <sys/socket.h> #include <pthread.h> #include <unistd.h&g…

AIGC中的视觉生成文献整理

文章目录 文件夹文献总览图像生成技术视频生成技术Video Generation with Text ConditionVideo Generation with other ConditionsVideo Editing 生成模型在其他任务上的应用扩散模型在数据标记上的应用可控的图像生成技术 文件夹文献总览 AIGC 视觉生成文献整理├── 图像生…

「递归算法」看这一篇就够了|多图

前言递归是一种非常重要的算法思想&#xff0c;无论你是前端开发&#xff0c;还是后端开发&#xff0c;都需要掌握它。在日常工作中&#xff0c;统计文件夹大小&#xff0c;解析xml文件等等&#xff0c;都需要用到递归算法。它太基础太重要了&#xff0c;这也是为什么面试的时候…

数组tostring方法_数组toString()方法以及JavaScript中的示例

数组tostring方法JavaScript toString()方法 (JavaScript toString() method) toString() method is used to convert an array to the string. It is called with the array name and returns the string containing array elements with comma separated. toString()方法用于…

JAVA解析JSON数据

在网页中想后台传递多个数据时&#xff0c;有时数据还是多个动态列表&#xff0c;数据很复杂时&#xff0c;JavaScript程序员喜欢把他们作为json串进行处理&#xff0c;后台收到后需要对json字符串进行解析&#xff0c;幸好有JSON-lib&#xff0c;这个Java类包用于把bean,map和…

为什么把端口号改为80之后,访问的时候就可以不写端口号

一个关于tomcat的问题为什么把端口号改为80之后&#xff0c;访问的时候就可以不写端口号因为80端口是许多web服务器的默认端口&#xff0c;比如iis和apache&#xff0c;所有为了方便,浏览器在不知道请求端口的情况下默认会访问服务器的80端口百度知道这样的话就可以恶作剧了打开…

linux——服务器与客户端实现聊天功能

先联想一下聊天的场景&#xff0c;假设甲和乙在聊天&#xff0c;他们每个人都能够发送给对方一句话甚至多句话&#xff0c;也能接收到对方发来的一句或多句话&#xff0c;也就是说&#xff0c;甲在发送一句话给乙的时候&#xff0c;同时也能接收到乙发来的信息&#xff0c;而且…

有关链表的小技巧,我都给你总结好了

链表链表是数据结构里一个很基础但是又很爱考的线性结构&#xff0c;链表的操作相对来说比较简单&#xff0c;但是非常适合考察面试者写代码的能力&#xff0c;以及对 corner case 的处理&#xff0c;还有指针的应用很容易引起 NPE (null pointer exception)。综合以上原因&…

long类型20位示例_Java Long类numberOfTrailingZeros()方法及示例

long类型20位示例长类numberOfTrailingZeros()方法 (Long class numberOfTrailingZeros() method) numberOfTrailingZeros() method is available in java.lang package. 在java.lang包中提供了numberOfTrailingZeros()方法 。 numberOfTrailingZeros() method is used to retu…

ActiveReports 9实战教程(1): 手把手搭建环境Visual Studio 2013 社区版

ActiveReports 9刚刚发布3天&#xff0c;微软就发布了 Visual Studio Community 2013 开发环境。Visual Studio Community 2013 提供完整功能的 IDE &#xff0c;可开发 Windows、Android 和 iOS 应用。支持&#xff1a;C, Python, HTML5, JavaScript, 和 C#,VB, F# 语言的开发…

第 1-1 课:Java 程序是如何执行的?

了解任何一门语言的精髓都是先俯览其全貌&#xff0c;从宏观的视角把握全局&#xff0c;然后再深入每个知识点逐个击破&#xff0c;这样就可以深入而快速的掌握一项技能。同样学习 Java 也是如此&#xff0c;本节就让我们先从整体来看一下 Java 中的精髓。 Java 介绍 Java 诞…

linux——两个客户端之间实现聊天(TCP、单线程)

两个客户端实现聊天功能&#xff0c;那么服务器作转发信息的作用&#xff0c;客户端A先将信息发送到服务器&#xff0c;在由服务器将信息发送到客户端B&#xff0c;客户端B也是一样。客户端与服务器都应该有两个执行流&#xff0c;服务器的一个执行流不断的接收客户端A的信息并…

Java ClassLoader getSystemClassLoader()方法与示例

ClassLoader类getSystemClassLoader()方法 (ClassLoader Class getSystemClassLoader() method) getSystemClassLoader() method is available in java.lang package. getSystemClassLoader()方法在java.lang包中可用。 getSystemClassLoader() method is used to find the Sys…

zabbix邮件通知,短信通知配置详解

一、使用邮件发送报警1、前提条件是zabbix我们已经安装完成2、在官网我们下载msmtp的文件http://sourceforge.net/projects/msmtp/files/msmtp/1.4.32/msmtp-1.4.32.tar.bz2/download tar xf msmtp-1.4.32.tar.bz2 cd msmtp-1.4.32 ./configure--prefix/usr/local/msmtp make m…

关于怎么获取jsp的web站点的目录问题

发布了自己的web站点之后&#xff0c;想要访问站点文件夹下的某个文本文件&#xff0c;但是却不知道怎么找到文件根目录&#xff0c;一直尝试总是找不到文件……好不容易在网上翻了堆代码找到两句我现在急用的……so//根目录路径的获取System.out.println(request.getSession()…

开篇词:如何轻松获得 Offer

你好,我是王磊,某上市公司技术研发经理,前奇虎 360 员工,有着 10 余年的编程工作经验,目前主要负责新员工技术面试和构建企业技术架构的相关事宜。随着面试过的人数增加,我发现面试者们暴露出了技术方面的很多问题,为了让更多面试者少走一些弯路,也为了让企业能招到合适…

Java类类getGenericSuperclass()方法及示例

类类getGenericSuperclass()方法 (Class class getGenericSuperclass() method) getGenericSuperclass() method is available in java.lang package. getGenericSuperclass()方法在java.lang包中可用。 getGenericSuperclass() method is used to return the Type denoting th…

linux——客户端服务器文件传输

实现文件传输并不难&#xff0c;只需用fopen、fread、fwrite、fclose这几个函数对文件操作即可。文本文件就不说了&#xff0c;我们就已下图为例。 我们先来看看这个图片文件里装的是什么&#xff0c;我们以notpad打开这个图片&#xff0c;结果如下&#xff0c;是一堆乱码。 …

Android第二十五期 - 猜歌小游戏

代码已经整理好了&#xff0c;如下效果图&#xff1a;地址&#xff1a;http://yunpan.cn/cArkdixh5NbpQ 提取码 9300转载于:https://blog.51cto.com/liangxiao/1579433