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
| class Solution { public: int clumsy(int N) { stack<int> stk; stk.push(N--); int idx = -1; while (N) { switch (idx = ++idx % 4) { case 0: stk.top() *= N--; break; case 1: stk.top() /= N--; break; case 2: stk.push(N--); break; case 3: stk.push(-(N--)); } } int sum = 0; while (!stk.empty()) { sum += stk.top(); stk.pop(); } return sum; } };
|