C ++程序找到序列1 + 1/2 ^ 2 + 1/3 ^ 3 +….. + 1 / n ^ n的和
在本教程中,我们将讨论一个程序,用于查找给定序列1+1/2^2+1/3^3+…..+1/n^n的总和。
为此,我们将得到n的值,我们的任务是从第一个项开始将每个项相加,以找到给定级数的总和。
示例
#include <iostream>
#include <math.h>
using namespace std;
//calculating the sum of the series
double calc_sum(int n) {
int i;
double sum = 0.0, ser;
for (i = 1; i <= n; i++)
ser = 1/ pow(i, i);
sum += ser;
return sum;
}
int main() {
int n = 5;
double res = calc_sum(n);
cout << res << endl;
return 0;
}输出结果
0.00032
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便