fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. class MovingMagic {
  6. public:
  7. void updateValue(double x) {
  8. if (!init_) {
  9. init_ = true;
  10. cv_ = x;
  11. } else {
  12. cv_ = cv_ * 0.999 + x * 0.001;
  13. }
  14. }
  15.  
  16. double getValue() {
  17. return cv_;
  18. }
  19. private:
  20. bool init_{false};
  21. double cv_{0};
  22. };
  23.  
  24. int main() {
  25. MovingMagic magic;
  26. double sum = 0;
  27. for (size_t i = 10000; i > 0; --i) {
  28. double value = i;
  29. magic.updateValue(value);
  30. sum += value;
  31. }
  32. std::cout << magic.getValue() << " " << sum / 10000 << std::endl;
  33. return 0;
  34. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
999.955 5000.5