forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
48 lines (40 loc) · 732 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class MovingAverage {
private:
queue<int> q;
int size;
double sum;
public:
MovingAverage(int size) {
this->size = size;
sum = 0;
}
double next(int val) {
if (q.size() >= size) {
sum -= q.front(); q.pop();
}
q.push(val);
sum += val;
return sum / q.size();
}
};
int main() {
MovingAverage m(3);
cout << m.next(1) << endl;
cout << m.next(10) << endl;
cout << m.next(3) << endl;
cout << m.next(5) << endl;
return 0;
}