fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <cmath>
  4. #include <numeric>
  5. using namespace std;
  6.  
  7. double power(double mid,int nth,double number){
  8. double result=1.0;
  9. for(int i=1;i<=nth;i++){
  10. result*=mid;
  11. }
  12. return result;
  13. }
  14.  
  15. double nthroot(int nth,double number){
  16. double low=1.0;
  17. double high=number;
  18. double eps=1e-6;
  19. while(high-low > eps ){
  20. double mid=(low+high)/2.0;
  21. if(power(mid,nth,number)==number){
  22. return mid;
  23. }
  24. else if(power(mid,nth,number)>number){
  25. high=mid;
  26. }
  27.  
  28. else{
  29. low=mid;
  30. }
  31. }
  32. return low;
  33. }
  34.  
  35. int main(){
  36. int nth;
  37. double number;
  38. cin>>nth;
  39.  
  40. cin>>number;
  41. double ans=nthroot(nth,number);
  42. cout<<nth<<"th root of "<<number<<": "<<ans<<endl;
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 5312KB
stdin
3
81
stdout
3th root of 81: 4.32675