题解 | #特殊的产奶量# 浮点数二分
特殊的产奶量
https://www.nowcoder.com/practice/7780f80e7ece45928c7c138a20fede91
知识点
二分
思路
不断二分找到根号x的值,为了防止死循环可以设定好次数
AC Code(c++)
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param x int整型
* @return string字符串
*/
string mySqrt(int x) {
// 浮点数二分
double l = 0.0, r = x;
int T = 100000000;
while (T --) {
if (r - l < 0.0001) break;
double mid = (l + r) / 2;
if (mid * mid > 1.0 * x) r = mid;
else l = mid;
}
string res = to_string(r);
for (int i = 0; i < 4; i ++) res.pop_back();
return res;
}
};
