有一分数序列: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;
}