已知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; else cout << (1 + (q - pow(q, n+1))/(1 - q)) << endl; system("pause");//向OJ提交時,注釋此句 return 0;}
新聞熱點
疑難解答