fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int N = 114067; // input value, can be read with scanf()
  6. int currentLength = 0; // here we will store the current number of consecutive ones
  7. int bestLength = 0; // here we will store the best result
  8.  
  9. while (N) // as long as N is greater than 1
  10. {
  11. if (N&1) // if the last bit is set to 1
  12. {
  13. // cool, let's increment the current sequence's length
  14. currentLength += 1;
  15.  
  16. // currentLength has changed, maybe now it is better than best known solution?
  17. if (currentLength > bestLength)
  18. {
  19. // awesome! new best solution is found
  20. bestLength = currentLength;
  21. }
  22. }
  23. else
  24. {
  25. // we have encountered 0 while scanning bits, we must start counting the length over
  26. currentLength = 0;
  27. }
  28.  
  29. // let's move to the next bit!
  30. N = N>>1;
  31. }
  32.  
  33. printf("%d", bestLength); // print out the value
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 2296KB
stdin
Standard input is empty
stdout
4