[关闭]
@Moritz 2019-03-29T08:49:51.000000Z 字数 1285 阅读 422

10.付账问题

蓝桥杯 蓝桥杯第九届 C++ 贪心 所有文稿


  1. /*16:13 start*/
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn=50000+10;
  8. int a[maxn];
  9. double b[maxn];
  10. double bzc(int n){
  11. double tot_b=0.0,s0=0.0;
  12. for(int i=1;i<=n;i++){
  13. tot_b+=b[i];
  14. }
  15. tot_b=tot_b/(n*1.0);
  16. cout<<endl<<"tot_b="<<tot_b<<endl;
  17. for(int i=1;i<=n;i++){
  18. s0=s0+(b[i]-tot_b)*1.0*(b[i]-tot_b);
  19. cout<<"i="<<i<<" "<<(b[i]-tot_b)<<endl;
  20. }
  21. cout<<"s0="<<s0<<endl;
  22. return sqrt(s0/(1.0*n));
  23. }
  24. int main(){
  25. int n;
  26. double s;
  27. cin>>n>>s;
  28. for(int i=1;i<=n;i++) cin>>a[i];
  29. sort(a,a+n);
  30. double aver=s*1.0/n;
  31. int i=1;
  32. while(s>0){
  33. if (a[i]<aver) b[i]=a[i]*1.0;
  34. else b[i]=aver;
  35. s-=b[i];
  36. if (n>i){
  37. aver=s*1.0/(n-i);
  38. i++;}
  39. }
  40. for(int i=1;i<=n;i++) cout<<a[i]<<" ";
  41. cout<<endl;
  42. for(int i=1;i<=n;i++) cout<<b[i]<<" ";
  43. cout<<endl<<bzc(n)<<endl;
  44. double tot_b=0.0,s0=0.0;
  45. for(int i=1;i<=n;i++){
  46. tot_b+=b[i];
  47. }
  48. tot_b=tot_b/(n*1.0);
  49. cout<<endl<<"tot_b="<<tot_b<<endl;
  50. for(int i=1;i<=n;i++){
  51. s0=s0+(b[i]-tot_b)*1.0*(b[i]-tot_b);
  52. cout<<"i="<<i<<" "<<(b[i]-tot_b)<<endl;
  53. }
  54. cout<<"s0="<<sqrt(s0/(1.0*n))<<endl;
  55. system("pause");
  56. return 0;
  57. }

出现了很神奇的输出结果

  1. 5 2333
  2. 666 666 666 666 666
  3. 666 666 666 666 666
  4. 466.6 466.6 466.6 466.6 466.6
  5. tot_b=466.6
  6. i=1 0
  7. i=2 0
  8. i=3 5.68434e-014
  9. i=4 0
  10. i=5 0
  11. s0=3.23117e-027
  12. 2.54211e-014
  13. tot_b=466.6
  14. i=1 0
  15. i=2 0
  16. i=3 5.68434e-014
  17. i=4 0
  18. i=5 0
  19. s0=2.54211e-014
  20. 请按任意键继续. . .

i=5时的5.68434e-014是怎么来的???

保留四位小数的输出方式

  1. printf( "%.4lf", sqrt(r/n));

2019.3.3

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注