数列通项公式
an=1.0 / (5.0 * (2.0 * n - 1.0)) - 1.0 / (5.0 * 2.0 * n)
输入项数n,求
f(n)=a1+a2+a3+...+an
结果保留4位小数。
#include<iostream>
using namespace std;
double sum = 0.0;
double f(int n)
{
for (int i = 1; i < n + 1; i++)
sum += 1.0 / (5.0 * (2.0 * i - 1.0)) - 1.0 / (5.0 * 2.0 * i);
return sum;
}
int main()
{
int num = 0;
while (1)
{
cin >> num;
double value = f(num);
printf_s("%.4f", value);
sum = 0;
cout << endl;
}
return 0;
}