fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int original(int turn) {
  6. if (turn != 0) {
  7. if (turn > 0) {
  8. turn /= 45;
  9. if (turn < 4) turn = 4;
  10. if (turn > 8) turn = 8;
  11. } else {
  12. turn /= 45;
  13. if (turn > -4) turn = -4;
  14. if (turn < -8) turn = -8;
  15. }
  16. }
  17. return turn;
  18. }
  19.  
  20.  
  21. int optimized(int turn) {
  22. int s=turn>>31,a=turn+s^s,b=(a>360?524288:a>180?a*1457:262144)>>16;
  23. return b+s^s;
  24. }
  25.  
  26.  
  27. int main() {
  28. int errors = 0;
  29. for (int i = -720; i < 720; ++i) if (i != 0) {
  30. int t1 = original(i);
  31. int t2 = optimized(i);
  32. if (t1 != t2) {
  33. std::cout << "at " << i << " expected " << t1 << " got " << t2 << std::endl;
  34. ++errors;
  35. }
  36. }
  37. std::cout << "errors: " << errors << std::endl;
  38. return 0;
  39. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
errors: 0