3.4 等比数列
已知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;elsecout << (1 + (q - pow(q, n+1))/(1 - q)) << endl;system("pause");//向OJ提交時(shí),注釋此句return 0; }
總結(jié)
- 上一篇: 3.3 1!到n!的和
- 下一篇: 3.5 斐波那契数