题目:
计算:
π/4=1-1/3+1/5-1/7+…,直到最后一项小于10-6
分析:
#include<iostream> using namespace std; void main() { double sum = 0; for (int i = 0;; i++) { double term = 1.0 / (i * 2.0 + 1.0); //正负号轮流求 if (i % 2 == 0) sum += term; else sum -= term; if (term < 1e-6) break; } cout << sum << endl; }