fork download
  1. #define a(x) (x>0?x:-(x))
  2. #define f(x,y) y>a(x-.5)?x--:-y>a(x+.5)?x++:x>a(y+.5)?y++:x|y?y--:x;
  3.  
  4. #include <iostream>
  5. void test(int x, int y, int rx, int ry){
  6. std::cout << "(" << x << ", " << y << ")=>";
  7. f(x,y);
  8. std::cout << "(" << x << ", " << y << ") - " << ((x==rx&&y==ry)?"OK":"FAILURE") << std::endl;
  9. }
  10.  
  11. int main() {
  12. test(0, 0, 0, 0);
  13. test(1, 0, 1, 1);
  14. test(1, 1, 0, 1);
  15. test(0, 1, -1, 1);
  16. test(-1, 1, -1, 0);
  17. test(-1, 0, -1, -1);
  18. test(-1, -1, 0, -1);
  19. test(0, -1, 1, -1);
  20. test(1, -1, 1, 0);
  21. test(95, -12, 95, -11);
  22. test(127, 127, 126, 127);
  23. test(-2, 101, -3, 101);
  24. test(-65, 65, -65, 64);
  25. test(-127, 42, -127, 41);
  26. test(-9, -9, -8, -9);
  27. test(126, -127, 127, -127);
  28. test(105, -105, 105, -104);
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
(0, 0)=>(0, 0) - OK
(1, 0)=>(1, 1) - OK
(1, 1)=>(0, 1) - OK
(0, 1)=>(-1, 1) - OK
(-1, 1)=>(-1, 0) - OK
(-1, 0)=>(-1, -1) - OK
(-1, -1)=>(0, -1) - OK
(0, -1)=>(1, -1) - OK
(1, -1)=>(1, 0) - OK
(95, -12)=>(95, -11) - OK
(127, 127)=>(126, 127) - OK
(-2, 101)=>(-3, 101) - OK
(-65, 65)=>(-65, 64) - OK
(-127, 42)=>(-127, 41) - OK
(-9, -9)=>(-8, -9) - OK
(126, -127)=>(127, -127) - OK
(105, -105)=>(105, -104) - OK