@Moritz
2019-03-29T08:49:51.000000Z
字数 1285
阅读 499
蓝桥杯 蓝桥杯第九届 C++ 贪心 所有文稿
/*16:13 start*/#include <iostream>#include <iomanip>#include <cmath>#include <algorithm>using namespace std;const int maxn=50000+10;int a[maxn];double b[maxn];double bzc(int n){double tot_b=0.0,s0=0.0;for(int i=1;i<=n;i++){tot_b+=b[i];}tot_b=tot_b/(n*1.0);cout<<endl<<"tot_b="<<tot_b<<endl;for(int i=1;i<=n;i++){s0=s0+(b[i]-tot_b)*1.0*(b[i]-tot_b);cout<<"i="<<i<<" "<<(b[i]-tot_b)<<endl;}cout<<"s0="<<s0<<endl;return sqrt(s0/(1.0*n));}int main(){int n;double s;cin>>n>>s;for(int i=1;i<=n;i++) cin>>a[i];sort(a,a+n);double aver=s*1.0/n;int i=1;while(s>0){if (a[i]<aver) b[i]=a[i]*1.0;else b[i]=aver;s-=b[i];if (n>i){aver=s*1.0/(n-i);i++;}}for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;for(int i=1;i<=n;i++) cout<<b[i]<<" ";cout<<endl<<bzc(n)<<endl;double tot_b=0.0,s0=0.0;for(int i=1;i<=n;i++){tot_b+=b[i];}tot_b=tot_b/(n*1.0);cout<<endl<<"tot_b="<<tot_b<<endl;for(int i=1;i<=n;i++){s0=s0+(b[i]-tot_b)*1.0*(b[i]-tot_b);cout<<"i="<<i<<" "<<(b[i]-tot_b)<<endl;}cout<<"s0="<<sqrt(s0/(1.0*n))<<endl;system("pause");return 0;}
出现了很神奇的输出结果
5 2333666 666 666 666 666666 666 666 666 666466.6 466.6 466.6 466.6 466.6tot_b=466.6i=1 0i=2 0i=3 5.68434e-014i=4 0i=5 0s0=3.23117e-0272.54211e-014tot_b=466.6i=1 0i=2 0i=3 5.68434e-014i=4 0i=5 0s0=2.54211e-014请按任意键继续. . .
i=5时的5.68434e-014是怎么来的???
保留四位小数的输出方式
printf( "%.4lf", sqrt(r/n));
2019.3.3
