#include <iostream>
using namespace std;
int main() {
int val;
cout << "Enter the number: ";
cin >> val;
if( val< 0) {
cout<< "According to my maths its not possible." << endl;
} else {
float low = 0, high = val;
float mid = (low + high)/2;
int c = 0;
while (c != 1) {
if(mid * mid = val) {
cout << "Square root is: " << mid <<endl;
c = 1;
} else {
if(mid * mid > val) {
high = mid;
mid = (low + high)/2;
} else {
low = mid;
mid = (low + high)/2;
}
}
}
}
return 0;
}
编写代码以在C ++中使用二选方法查找方形 - 根? [关闭
-
29-07-2022 - |
题
据我所知,一分为二是缩小您的搜索并在间隔中达到特定值的范围。请给我一个示例,如何制作通用代码以找到方形根。我认为的方式是将三个变量低,中高。 high = userInput,低= 0,中间(低 +高) /2,问题是如何更改值。
解决方案
其他提示
假设我们正在寻找 sqrt(N)
如上所述 这里, ,如果平均平均正方形大于 N
, ,如果少于我们刚发现的平均值,我们将高价值更改 N
, ,我们通过平均值更改低值。我们将步骤重复多次以满足所需的精度。
不隶属于 StackOverflow