多项式求和
题目分析:
一道经典的入门算法题,利用for循环以及-1的累乘,进行计算,代码如下
代码:
#include<iostream> using namespace std; int main(){ int i, n; int t = 1; int cnt; double sum, num; while (cin >> n) { while (n--) { cin >> cnt; sum = 0; t = 1; for (i = 1; i <= cnt; i++) { num = (double)t / (double)i; sum = sum + num; t = t*-1; } printf("%0.2lf ", sum); } } system("pause"); return 0; }