Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DuckSoft/9132dcee852312890172de801a6227f1 to your computer and use it in GitHub Desktop.
Save DuckSoft/9132dcee852312890172de801a6227f1 to your computer and use it in GitHub Desktop.
Nowcoder 求数列的和
// 精度取舍
#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