fork(1) download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. unsigned long long stringToNumber(string const& s)
  7. {
  8. size_t i = 0;
  9. unsigned long long num = 0;
  10. for (auto it = s.rbegin(); it != s.rend(); ++it, ++i )
  11. {
  12. if (*it != '0')
  13. {
  14. num += 1ULL << i;
  15. }
  16. }
  17.  
  18. return num;
  19. }
  20.  
  21. int getStepsTo1(unsigned long long num)
  22. {
  23. long int count = 0;
  24. while (num != 1 )
  25. {
  26. if (num % 2 == 0)
  27. num /= 2;
  28. else
  29. num += 1;
  30. count += 1;
  31. }
  32. return count;
  33. }
  34.  
  35. int main()
  36. {
  37. string s = "1111011110000011100000110001011011110010111001010111110001";
  38. unsigned long long int dec = stringToNumber(s);
  39. cout << "Number: " << dec << endl;
  40. // dec = 278675673186014705;
  41.  
  42. int count = getStepsTo1(dec);
  43. cout << "Steps to 1: " << count << endl;
  44. return 0;
  45. }
  46.  
  47.  
Success #stdin #stdout 0.01s 5520KB
stdin
Standard input is empty
stdout
Number: 278675673186014705
Steps to 1: 85