已知q與n,求等比數列之和:1+ q + q^2 + ... + q^n
輸入樣例:
6 ?0.3 ?5 ?1.3
輸出樣例:
1.428
12.756
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;int main()
{ifstream cin("test.txt");//向OJ提交時,注釋此句int n;double q;//加fixed,表示定點輸出小數點后三位;否則輸出三位有效數字cout.precision(3); cout << fixed;//設置定點輸出while (cin >> n >> q)if (q == 1)cout << n + 1 << endl;elsecout << (1 + (q - pow(q, n+1))/(1 - q)) << endl;system("pause");//向OJ提交時,注釋此句return 0;
}