C/C++ | 23-13 求此数列前20项的和

来源:互联网 发布:好听的网络名字 编辑:程序博客网 时间:2024/06/05 06:44


有一分数序列:1/2,1/4,1/6,1/8……,用函数调用的方法,求此数列前20项的和


#include <cstdio>#include <deque>#include <algorithm>#include <iterator>#include <stdio.h>  #include <stdlib.h>#include <iostream>#include <string.h>#include <assert.h>using namespace std;double getval(int n){double result = 0.0;for (int i = 1; i<=n ; i++)result = result + 1.0 / (i*2.0);return result;}int main(){int n = 20;double x = getval(n);cout << x << endl;system("pause");return 0;}


阅读全文
0 0
原创粉丝点击