fork download
  1. #include <iostream>
  2. #include <random>
  3. #include <cstdint>
  4. #include <limits>
  5.  
  6. int AboutX(std::uint32_t X, std::uint32_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::uint32_t X, std::uint32_t Add, int Judge){
  13. if (Add == 0) Add = 1;
  14. if (Judge == 1) return X + Add;
  15. if (Judge == -1) return X - Add;
  16. return X;
  17. }
  18.  
  19. int main(){
  20. std::random_device rd;
  21. std::mt19937 mt(rd());
  22. std::uniform_int_distribution<std::uint32_t> uid;
  23.  
  24. std::uint32_t X = std::numeric_limits<std::uint32_t>::max();
  25. std::uint32_t Add = X;
  26. std::uint32_t A = uid(mt);
  27. int i = 0;
  28. int Judge = -1;
  29. do{
  30. Add /= 2;
  31. X = BinarySearch(X, Add, Judge);
  32. Judge = AboutX(X, A);
  33. i++;
  34. } while (Judge != 0);
  35.  
  36. std::cout << "X=>" << X << ',' << "Answer=>" << A << ' ' << std::endl << "Search for "<<i<<" Count!" << std::endl<< "this is " << ((A == X) ? "Valid" : "Invalid") << std::endl;
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
X=>1067887816,Answer=>1067887816 
Search for 31 Count!
this is Valid