题目:1063 计算谱半径 (20 分)
在数学中,矩阵的“谱半径”是指其特征值的模集合的上确界。换言之,对于给定的 n 个复数空间的特征值 { , },它们的模为实部与虚部的平方和的开方,而“谱半径”就是最大模。
现在给定一些复数空间的特征值,请你计算并输出这些特征值的谱半径。
输入格式:
输入第一行给出正整数 N(≤ 10 000)是输入的特征值的个数。随后 N 行,每行给出 1 个特征值的实部和虚部,其间以空格分隔。注意:题目保证实部和虚部均为绝对值不超过 1000 的整数。
输出格式:
在一行中输出谱半径,四舍五入保留小数点后 2 位。
输入样例:
5 0 1 2 0 -1 0 3 3 0 -3输出样例:
4.24
思路:
- 简单题,按着题目要求做就行。开根号用sqrt()函数,幂级计算用pow(底数,指数)。
代码:
1 #include <cstdio>
2 #include <cstring>
3 #include <cctype>
4 #include <iostream>
5 #include <sstream>
6 #include <cmath>
7 #include <algorithm>
8 #include <string>
9 #include <stack>
10 #include <queue>
11 #include <vector>
12 #include <map>
13 using namespace std;
14
15 int main()
16 {
17 int n;
18 scanf("%d", &n);
19 double maxx = 0;
20 while(n--)
21 {
22 int x, y;
23 scanf("%d %d", &x, &y);
24 double r = sqrt(pow(x, 2) + pow(y, 2));
25 if(r > maxx)
26 maxx = r;
27 }
28 printf("%.2lf", maxx);
29 return 0;
30 }
总结:
最后输出是个浮点型,我居然糊涂的给设成了整型变量。所以0.00 。