PAT乙級-1063. 計算譜半徑(20)
阿新 • • 發佈:2017-08-26
log 顯示 輸入 最大 pre fix 需要 blog esp
在數學中,矩陣的“譜半徑”是指其特征值的模集合的上確界。換言之,對於給定的n個復數空間的特征值{a1+b1i, ..., an+bni},它們的模為實部與虛部的平方和的開方,而“譜半徑”就是最大模。
現在給定一些復數空間的特征值,請你計算並輸出這些特征值的譜半徑。
輸入格式:
輸入第一行給出正整數N(<= 10000)是輸入的特征值的個數。隨後N行,每行給出1個特征值的實部和虛部,其間以空格分隔。註意:題目保證實部和虛部均為絕對值不超過1000的整數。
輸出格式:
在一行中輸出譜半徑,四舍五入保留小數點後2位。
輸入樣例:
5 0 1 2 0 -1 0 3 3 0 -3
輸出樣例:
4.24
分析:
冪函數 pow(a,n);
開方函數 double sqrt(double)
別人家的代碼
#include<iostream> #include<math.h> #include<iomanip> using namespace std; int main(){ int N; double a,b; double max=0; cin>>N; while(N--){ cin>>a>>b; double temp=sqrt(pow(a,2)+pow(b,2)); if(temp>max){ max=temp; } } //max+=0.005;//並不需要這個 自動四舍五入 cout<<fixed<<setprecision(2)<<max; }
自己家的代碼
#include<iostream> #include<math.h> #include<iomanip> using namespace std; int main(){ int n; double a[10001]; cin >> n; double n1, n2; for (inti = 0; i < n; i++) { cin >> n1 >> n2; a[i] = sqrt(n1*n1 + n2*n2); } double max = -1; for (int i = 0; i < n; i++) { if (a[i]>max) max = a[i]; } cout <<fixed<<setprecision(2)<< max; return 0; }
最後Emmmm 關於fixed 固定點方式顯示
以下是搜索的CSDNget到的答案
#include <iostream> #include <iomanip> using namespace std; int main( void ) { const double value = 12.3456789; cout << value << endl; // 默認以6精度,所以輸出為 12.3457 cout << setprecision(4) << value << endl; // 改成4精度,所以輸出為12.35 cout << setprecision(8) << value << endl; // 改成8精度,所以輸出為12.345679 cout << fixed << setprecision(4) << value << endl; // 加了fixed意味著是固定點方式顯示,所以這裏的精度指的是小數位,輸出為12.3457 cout << value << endl; // fixed和setprecision的作用還在,依然顯示12.3457 cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢復成整個數值的有效位數,顯示為12.35 cout << value << endl; cout.precision( 6 ); // 恢復成原來的樣子,輸出為12.3457 cout << value << endl; }
PAT乙級-1063. 計算譜半徑(20)