fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool power(double a, double n, double m){
  5. double res = 1;
  6. for (int i = 1; i <= n; i++){
  7. res *= a;
  8. if(res > m) return false;
  9. }
  10.  
  11. return true;
  12.  
  13. }
  14.  
  15. double findNthRootOfM(int n, int m){
  16. double l = 1, r = m;
  17. while(r-l > 1e-6){
  18. double mid = (l + r)/2;
  19. if(power(mid, n, m)){
  20. l = mid;
  21. }
  22. else r = mid;
  23. }
  24.  
  25. return l;
  26. }
  27.  
  28.  
  29. int main() {
  30. // your code goes here
  31. cout << findNthRootOfM(3, 27);
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
3