PAT Level B-Calculate the spectral radius

Title description
In mathematics, the "spectral radius" of a matrix refers to the supremum of the set of modules of its eigenvalues.

In other words, for a given n complex number space eigenvalues ​​{a ​1 ​​+b ​1 ​​i, …, a ​n ​​+b ​n ​​i },

Their modulus is the square root of the sum of squares of the real and imaginary parts, and the "spectral radius" is the maximum modulus.

Now given some eigenvalues ​​in complex space, please calculate and output the spectral radius of these eigenvalues.

Input format The
first line of input gives a positive integer N, which is the number of input eigenvalues.
Next N lines, each line gives the real and imaginary parts of 1 eigenvalue, separated by spaces.

Note: The question guarantees that the real and imaginary parts are integers whose absolute value does not exceed 1000.

Output format
Output the spectrum radius in one line, rounded to the nearest 2 digits after the decimal point.

Input example
5
0 1
2 0
-1 0
3 3
0 -3

Sample output
4.24

Data range
N ≤ 10 000


Problem solution
mathematics:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

double ans;
int n, a, b;

int main()
{
    
    
	cin >> n;
	
	while(n --)
	{
    
    
		cin >> a >> b;
		ans = max(ans, sqrt(a * a + b * b));
	}
	
	printf("%.2f", ans);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/113850447