fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int n = 5;
  7. int op_limit = 1;
  8. int op_done = 0;
  9. bool add = true;
  10. int sum = 0;
  11. for(int i = 1; i <= n; ++i) {
  12. if(op_done == op_limit) {
  13. op_done = 0;
  14. ++op_limit;
  15. add = !add;
  16. }
  17. sum += (add? i : -i);
  18. ++op_done;
  19. }
  20. std::cout << sum;
  21. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
5