fork(1) download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <cstdint>
  5. #include <limits>
  6.  
  7. int CountBit(std::int64_t N){
  8. int C = 0;
  9.  
  10. for (std::size_t i = 0; i < std::numeric_limits<std::int64_t>::digits; i++){
  11. if ((N&((1ull << i)))>0) C++;
  12. }
  13.  
  14. return C;
  15. }
  16.  
  17. std::string ltostr(std::int64_t N){
  18. std::string str;
  19. bool F = false;
  20. std::string C = "01";
  21. for (size_t i = std::numeric_limits<std::int64_t>::digits+1;i>0; i--){
  22. if ((N&(1ull << (i-1))) == 0 && F == false) continue;
  23. F = true;
  24. str += C[(N&(1ull << (i - 1))) > 0];
  25. }
  26.  
  27. return str;
  28. }
  29.  
  30. std::string MakeHoge(std::string Bit){
  31. char* P = NULL;
  32. std::int64_t N =std::strtol(Bit.c_str(), &P, 2);//C99
  33. if (*P != '\0') return "Error";
  34. std::string R;
  35. for (std::int64_t i = N+1; i < std::numeric_limits<std::int64_t>::max(); i++)
  36. {
  37. //R = ltostr(N);//selfmade
  38. if (CountBit(i) == 3){
  39. R = ltostr(i);//selfmade
  40. break;
  41. }
  42. }
  43. return R;
  44. }
  45.  
  46. bool Show(std::string P, std::string S){
  47. std::cout << P << " -> " << S << std::endl;
  48.  
  49. return true;
  50.  
  51. }
  52.  
  53. int main(){
  54. std::int64_t MagicWord = 0;
  55. std::string P;
  56. std::string S;
  57.  
  58. P = "111";
  59. S = MakeHoge(P);
  60. Show(P, S);
  61.  
  62. P = "1110";
  63. S = MakeHoge(P);
  64. Show(P, S);
  65.  
  66. P = "110100";
  67. S = MakeHoge(P);
  68. Show(P, S);
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
111 -> 1011
1110 -> 10011
110100 -> 111000