C++设置输出小数位的函数
#include<iomanip>
cout << setiosfalgs(ios::fixed)<<setprecision(2)<<2.119;
#include<iostream> #include<vector> #include<iomanip> using namespace std; int main() { int num; cin>>num; int *test=new int[num]; int tmp=0; for(int i=0;i<num;i++) cin >> test[i]; vector<int> A1,A2,A3,A4,A5; for(int i=0;i<num;i++) { tmp=test[i]%5; switch(tmp) { case 0: if(test[i]%10==0) A1.push_back(test[i]); break; case 1: A2.push_back(test[i]); break; case 2: A3.push_back(test[i]); break; case 3: A4.push_back(test[i]); break; case 4: A5.push_back(test[i]); break; default : break; } } double t_f=0.0; cout<<setiosflags(ios::fixed)<<setprecision(1); if(A1.size()==0) cout<<"N "; else { tmp=0; for(int i=0;i<A1.size();i++) if(A1[i]%2==0) tmp+=A1[i]; cout <<tmp<<" "; } if(A2.size()==0) cout<<"N "; else { int k=-1; tmp=0; for(int i=0;i<A2.size();i++) { k=0-k; tmp+=A2[i]*k; } cout<<tmp<<" "; } if(A3.size()==0) cout<<"N "; else cout <<A3.size()<<" "; if(A4.size()==0) cout<<"N "; else { tmp=0; for(int i=0;i<A4.size();i++) tmp+=A4[i]; t_f=tmp*1.00/A4.size(); cout<<t_f<<" "; } if(A5.size()==0) cout<<"N "; else { tmp=0; for(int i=0;i<A5.size();i++) if(A5[i]>tmp) tmp=A5[i]; cout<<tmp<<endl; } delete [] test; return 0; }