Created
June 6, 2018 16:42
-
-
Save DuckSoft/9132dcee852312890172de801a6227f1 to your computer and use it in GitHub Desktop.
Nowcoder 求数列的和
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 精度取舍 | |
#include<iostream> | |
#include<iomanip> | |
#include<cmath> | |
using namespace std; | |
inline double func(int a, int n) { | |
return pow(a, pow(2, 1-n)); | |
} | |
inline double calc(int n, int m) { | |
double sum = 0.0; | |
for (int i = 1; i <= m; ++i) { | |
double delta = func(n, i); | |
if (delta <= 0.001) return sum; | |
sum += delta; | |
} | |
return sum; | |
} | |
int main() { | |
cout << setiosflags(ios::fixed) << setprecision(2); | |
int n, m; while (cin >> n >> m) { | |
cout << calc(n, m) << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment