Problem Description
多项式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + …
现在请你求出该多项式的前n项的和。
Input
输入数据由2行组成,首先是一个正整数m(m<100),表示测试实例的个数,第二行包含m个正整数,对于每一个整数(不妨设为n, n<1000),求该多项式的前n项的和。
Output
对于每个测试实例n,要求输出多项式前n项的和。每个测试实例的输出占一行,结果保留2位小数。
Sample Input
2
1 2
Sample Output
1.00
0.50
参考代码如下:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int m,n,i;
float s;
while(cin>>m)
{
while(m--)
{
s=0;
cin>>n;
for(i=1; i<=n; i++)
s += ( 1 / ( ( pow( (-1), (i+1) ) ) * i ) ); // 相当于乘以(-1)的i+1次方
cout << setiosflags(ios::fixed) << setprecision(2) << s;
cout << endl;
}
}
return 0;
}
其中setiosflags() , setprecision()函数在我转载的一篇博文里面有详细讲解
https://blog.csdn.net/weixin_43469047/article/details/83422790