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