2023-08-14 15:58:35
发布于:广东
#include <bits/stdc++.h>
using namespace std;
double f(double x, double epsilon = 1e-6) {
if (x == 0 || x == 1) {
return x;
}
double left = 0, right = x;
while (right - left > epsilon) {
double mid = (left + right) / 2;
double square = mid * mid;
if (square > x) {
right = mid;
} else {
left = mid;
}
}
double ans = (left + right) / 2;
if(ans>0) return ans;
else return -1;
}
int main() {
double number;
cin >> number;
double result = f(number);
if(result!=-1) printf("%.5lf", result);
else cout<<-1;
return 0;
}
这里空空如也
有帮助,赞一个