fork download
  1. #include <iostream>
  2. #include <random>
  3. #include <cstdint>
  4. #include <limits>
  5.  
  6. int AboutX(std::int32_t X, std::int32_t A){
  7. if (A > X)return 1;
  8. if (A < X) return -1;
  9. return 0;
  10. }
  11.  
  12. std::uint32_t BinarySearch(std::int32_t X, std::int32_t Add, int Judge){
  13. int F = 1;
  14. if (Add < 0) F *= -1;
  15. if (Add == 0) Add = 1*F;
  16. if (Judge == F) return X + Add;
  17. if (Judge == -F) return X - Add;
  18. return X;
  19. }
  20.  
  21. std::pair<std::int32_t,int> Process(int A){
  22. std::int32_t X = 0;
  23. std::int32_t Add = 0;
  24. int i = 0;
  25. int Judge = 0;
  26. do{
  27. if (i == 1){
  28. if (Judge > 0){
  29. Add = std::numeric_limits<std::int32_t>::max();
  30. }
  31. else{
  32. Add = std::numeric_limits<std::int32_t>::min();
  33. }
  34. }
  35. Add /= 2;
  36. X = BinarySearch(X, Add, Judge);
  37. Judge = AboutX(X, A);
  38. i++;
  39. } while (Judge != 0);
  40.  
  41. return std::make_pair(X,i);
  42. }
  43.  
  44. int main(){
  45. std::random_device rd;
  46. std::mt19937 mt(rd());
  47. //std::uniform_int_distribution<std::int32_t> uid(std::numeric_limits<std::int32_t>::min(),std::numeric_limits<std::int32_t>::max());
  48. std::uniform_real_distribution<> urd;
  49. std::int32_t A1 = (std::int32_t)(std::numeric_limits<std::int32_t>::max()*urd(mt));
  50. auto X1 = Process(A1);
  51. std::int32_t A2 = (std::int32_t)(std::numeric_limits<std::int32_t>::min()*urd(mt));
  52. auto X2 = Process(A2);
  53.  
  54. std::cout << "X=>" << X1.first << ',' << "Answer=>" << A1 << ' ' << std::endl << "Search for "<<X1.second<<" Count!" << std::endl<< "this is " << ((A1 == X1.first) ? "Valid" : "Invalid") << std::endl;
  55. std::cout << "X=>" << X2.first << ',' << "Answer=>" << A2 << ' ' << std::endl << "Search for "<<X2.second<<" Count!" << std::endl<< "this is " << ((A2 == X2.first) ? "Valid" : "Invalid") << std::endl;
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
X=>1382432118,Answer=>1382432118 
Search for 31 Count!
this is Valid
X=>-1569586155,Answer=>-1569586155 
Search for 32 Count!
this is Valid