已知q與n,求等比數(shù)列之和: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提交時(shí),注釋此句 int n; double q; //加fixed,表示定點(diǎn)輸出小數(shù)點(diǎn)后三位;否則輸出三位有效數(shù)字 cout.PRecision(3); cout << fixed;//設(shè)置定點(diǎn)輸出 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提交時(shí),注釋此句 return 0;}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注