fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int timeToEnterMazeOptimized(int N) {
  5. if (N <= 0) return 0;
  6. if (N == 1 || N == 3) return 1;
  7.  
  8. int time = 0;
  9. while (N > 0) {
  10. time++;
  11. if (N == 1 || N == 3) return time;
  12.  
  13. if (N % 2 == 0) {
  14. N = N / 2;
  15. } else {
  16. N = (N + 1) / 2;
  17. }
  18. }
  19. return time;
  20. }
  21.  
  22. int main() {
  23. int N;
  24. cin >> N;
  25. cout << timeToEnterMazeOptimized(N) << endl;
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5316KB
stdin
9
stdout
3