Test question algorithm to improve programming to solve the root number 3 (C++)

Resource limit
Time limit: 1.0s Memory limit: 256.0MB
Problem description
  Performing mathematical operations is the main ability of computers. Using the computer's repetitive computing capabilities can help people solve some very complex mathematical operations, such as higher-order equations, roots, and squares. Wait. The radical operation is the basic operation of mathematics. For the solution of irrational numbers, we can use the method of taking the values ​​on both sides and continuously approximating the solution. Now you are required to program to solve the root number 3.
Input format
  no input
Output format
  output the solution result of root number 3
Sample input
None
Sample output
None The
data size and the agreed
  initial boundary values ​​are respectively: low=1.0, high=2.0, the update boundary value uses the average, and requires a loop of 20 Output the result after the second time.

#include<iostream>
using namespace std;
int main()
{
    
    
	double low = 1.0 ,high = 2.0 ;
	int i = 20;
	while(i--){
    
    
		double mid = (low+high)/2;
		if(mid*mid-3>0){
    
    
			high = mid;
		}else{
    
    
			low = mid;
		}
	}
	cout<<low;
	return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_51430516/article/details/115220206